【问题标题】:Problem with Firestore dependencies while exporting jar using Gradle使用 Gradle 导出 jar 时出现 Firestore 依赖项问题
【发布时间】:2019-10-01 04:44:40
【问题描述】:

我正在使用 Gradle 在 Eclipse 中构建一个应用程序。 当我通过从 Eclipse 运行来测试应用程序时,一切正常 - 但是,当我尝试导出并运行 .jar 文件时,我收到以下错误:

Failed for: .\ServiceAccountKey.json (The system cannot find the file specified)
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/firebase/cloud/FirestoreClient
    at main.Main.getSuroviny(Main.java:105)
    at main.Main.main(Main.java:39)
Caused by: java.lang.ClassNotFoundException:     com.google.firebase.cloud.FirestoreClient
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)

我的 build.gradle 文件如下所示:

apply plugin: 'java-library'
apply plugin: 'java'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    google()
}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their     own compile classpath.
    implementation 'com.google.guava:guava:23.0'
    implementation 'com.google.firebase:firebase-admin:5.8.0'
    implementation 'com.google.gms:google-services:4.0.1'

    implementation 'com.google.firebase:firebase-core:16.0.6'
    //dependency for using firebase database
    implementation 'com.google.firebase:firebase-database:16.0.6'
    //dependency for email and password authentication
    implementation 'com.google.firebase:firebase-auth:16.1.0'
    //dependency for cloud storage
    implementation 'com.google.firebase:firebase-storage:16.0.5'
    implementation 'com.google.firebase:firebase-firestore:18.0.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}


// Include dependent libraries in archive.

jar {
  manifest { 
    attributes "Main-Class": "main.Main"
  }  

  from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  }
}

我已经仔细检查了所有导入和实现,根据我在 Firestore 文档中找到的信息,一切似乎都是正确的。

另外,我已经尝试过一些事情,错误会在它找到的第一个 firstore 方法上发生变化 - 无论是 DocumentReference、FirestoreClient 还是其他任何方法。

感谢任何提前推荐的人。

【问题讨论】:

    标签: java eclipse firebase gradle google-cloud-firestore


    【解决方案1】:

    最后,错误是我的错误 - 我的 .jar 没有包含加载 Firestore 实例所需的服务密钥(出于某种原因,我希望 eclipse 在导出可运行的 jar 文件时包含它,因为我有选中“在包中包含资源文件”选项。

    因此,我将服务密钥 JSON 文件包含在我的 .jar 文件所在的同一文件夹中,因此我可以根据文件的位置读取它。

    这不是一个非常优雅的解决方法,但就目前而言,这就足够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 2022-11-29
      • 1970-01-01
      • 2018-01-02
      相关资源
      最近更新 更多