我正在使用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还是其他。

感谢任何事先提出建议的人。

最佳答案

最后,错误是我的错误-我的.jar不包含加载Firestore实例所需的Service键(出于某种原因,我希望eclipse在导出可运行jar文件时会包含它,因为我已经检查了“在包中包括资源文件”选项。

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

这不是一个很好的解决方法,但是到目前为止,就足够了。

07-24 09:48
查看更多