2017-02-14 32 views
8

我使用這個:

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); 

然而,應用程序崩潰與java.lang.NoClassDefFoundError for FirebaseOptions$Builder

build.gradle

dependencies { 
    testCompile group: 'junit', name: 'junit', version: '4.11' 
    compile 'com.google.firebase:firebase-admin:4.1.1' 
} 

我使用的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) 

我有我的應用程序的根firebase_key.json。

這是什麼造成的?

+0

添加您的logcat –

+0

沒想到它是需要的。添加 –

回答

4

的問題竟然是,我是用這個命令來構建:

gradlew clean install 

然而,由生成的Jar不包含依賴。 Firebase Admin SDK是一個依賴項。

所以我所做的是我使用shadowJar,它會生成一個包含依賴關係的Jar。不像gradlew clean install

然後,在Procfile中,我將它設置爲shadowJar生成的Jar。我看到的唯一問題是,現在我必須去IntelliJ並從那裏運行shadowJar,因爲似乎沒有任何命令可以從命令行運行它。

希望這有助於

1

你不是設置在gradle產出的火力點選擇正確的進口,就沒有必要對管理軟件包,你需要的核心:

而不是compile 'com.google.firebase:firebase-admin:4.1.1'

使用compile "com.google.firebase:firebase-core:10.0.1"