I'm using this:
FileInputStream serviceAccount;
try {
serviceAccount = new FileInputStream("firebase_key.json");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
return;
}
System.out.println("Reached here!");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl("https://*.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
However, the application crashes with a java.lang.NoClassDefFoundError for FirebaseOptions$Builder
My build.gradle
:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'com.google.firebase:firebase-admin:4.1.1'
}
I'm using IntelliJ.
Logcat:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/firebase/FirebaseOptions$Builder
10:57:43 AM web.1 | at com.x.*.TokenGenerator.main(TokenGenerator.java:26)
10:57:43 AM web.1 | Caused by: java.lang.ClassNotFoundException: com.google.firebase.FirebaseOptions$Builder
10:57:43 AM web.1 | at java.net.URLClassLoader.findClass(Unknown Source)
10:57:43 AM web.1 | at java.lang.ClassLoader.loadClass(Unknown Source)
10:57:43 AM web.1 | at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
10:57:43 AM web.1 | at java.lang.ClassLoader.loadClass(Unknown Source)
I have my firebase_key.json in the root of the app.
What's causing this?
The problem turned out to be that I was using this command to build:
gradlew clean install
However, the Jar generated by that doesn't contain the dependencies. And the Firebase Admin SDK is a dependency.
So what I did is I used shadowJar, which generates a Jar that contains dependencies. Unlike gradlew clean install
.
Then, in the Procfile, I set it to the Jar that shadowJar generates. The only problem I see to have is, now I have to go to IntelliJ and run the shadowJar from there, since there doesn't seem any command to run it from the commandline.
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With