使用FirebaseOptions.Builder将java连接到firebase时的java.lang.NoSuchMethodError

时间:2018-06-04 14:43:02

标签: java maven firebase

我已经将所有firebase maven依赖项jar文件(我们将groupID com.google.firebase与artifactID firebase-admin,版本6.2.0一起使用时获得)下载到动态Web项目中。 然后我使用firebase文档中给出的代码来连接到firebase。这是文档代码:

public class FirebaseUpdater {

public static void main(String[] args){
    FileInputStream serviceAccount;
    try {
        serviceAccount = new 
                FileInputStream("C:/Users/Asus/workspace/"
                        + "school1/schoolbackup_service.json");
        //FileInputStream("path/to/serviceAccountKey.json");

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://schoolbackup-f2429.firebaseio.com/")
                .build();

        FirebaseApp.initializeApp(options);

        // As an admin, the app has access to read and write all data, regardless of Security Rules
        DatabaseReference ref = FirebaseDatabase.getInstance()
            .getReference("restricted_access/secret_document");
        ref.addListenerForSingleValueEvent(new ValueEventListener() {

          public void onDataChange(DataSnapshot dataSnapshot) {
            Object document = dataSnapshot.getValue();
            System.out.println(document);
          }

          public void onCancelled(DatabaseError error) {
          }
        });

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

但是,我对FirebaseOptions.Builder函数收到以下错误:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.Iterables.getFirst(Ljava/lang/Iterable;Ljava/lang/Object;)Ljava/lang/Object;
at com.google.auth.oauth2.OAuth2Credentials.getFromServiceLoader(OAuth2Credentials.java:291)
at com.google.auth.oauth2.ServiceAccountCredentials.<init>(ServiceAccountCredentials.java:165)
at com.google.auth.oauth2.ServiceAccountCredentials.fromPkcs8(ServiceAccountCredentials.java:266)
at com.google.auth.oauth2.ServiceAccountCredentials.fromJson(ServiceAccountCredentials.java:195)
at com.google.auth.oauth2.GoogleCredentials.fromStream(GoogleCredentials.java:160)
at com.google.auth.oauth2.GoogleCredentials.fromStream(GoogleCredentials.java:127)
at FirebaseUpdater.main(FirebaseUpdater.java:25)

以下是包含com.google.firebase依赖关系列表的文件夹。它们都已添加到我的参考库中。

enter image description here

enter image description here

enter image description here

我做错了什么?它是一些丢失的罐子吗?罐子不应该在参考图书馆吗?我不应该为动态Web项目使用maven依赖项吗?或者是我甚至不能在非gradle和非maven项目中使用firebase连接?这个问题的解决方法是什么?因为我迫切需要Java-Firebase连接。

请帮忙!!!提前谢谢

0 个答案:

没有答案