如何解决从Gradle Fat Jar加载Kotlin主类失败
基于此处的其他答案,我正在使用此Gradle构建通过我的命令行工具和许多JDBC驱动程序来创建一个胖/超级/阴影的Jar:
import org.gradle.jvm.tasks.Jar
dependencies {
api(project(":db2xls"))
api("ch.qos.logback:logback-classic:1.2.3")
api("com.h2database:h2:1.4.200")
api("org.postgresql:postgresql:42.2.16")
api("MysqL:mysql-connector-java:8.0.21")
api("com.microsoft.sqlserver:mssql-jdbc:8.4.1.jre11")
}
val fatJar = task("fatJar",type = Jar::class) {
manifest {
attributes["Implementation-Title"] = "DB2XLS Bundle"
attributes["Implementation-Version"] = "0.1-SNAPSHOT"
attributes["Main-Class"] = "de.peterbecker.xls.Db2XlsKt"
}
from(
configurations.compile.get().map { if (it.isDirectory) it else zipTree(it) },configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) }
)
with(tasks.jar.get() as copySpec)
}
tasks {
"build" {
dependsOn(fatJar)
}
}
该版本会生成一个JAR文件,当使用ZIP工具进行检查时看起来不错,但无法执行:
$ java -jar db2xls-bundle-0.1-SNAPSHOT.jar
Error: Could not find or load main class de.peterbecker.xls.Db2XlsKt
Caused by: java.lang.classNotFoundException: de.peterbecker.xls.Db2XlsKt
如果我改用类路径,也是如此:
$ java -cp db2xls-bundle-0.1-SNAPSHOT.jar de.peterbecker.xls.Db2XlsKt
Error: Could not find or load main class de.peterbecker.xls.Db2XlsKt
Caused by: java.lang.classNotFoundException: de.peterbecker.xls.Db2XlsKt
该类文件似乎在那里,
$ jar -t --file db2xls-bundle-0.1-SNAPSHOT.jar | grep Db2XlsKt
de/peterbecker/xls/Db2XlsKt$logger$1.class
de/peterbecker/xls/Db2XlsKt$processQuery$1.class
de/peterbecker/xls/Db2XlsKt$toLists$$inlined$use$lambda$1.class
de/peterbecker/xls/Db2XlsKt.class
正常的JAR似乎找到了按预期加载的类(然后由于缺乏依赖而失败):
$ java -cp db2xls-0.1-SNAPSHOT.jar de.peterbecker.xls.Db2XlsKt
Error: Unable to initialize main class de.peterbecker.xls.Db2XlsKt
Caused by: java.lang.NoClassDefFoundError: kotlin/NowhenBranchMatchedException
我在做什么错了?
完整的代码位于https://github.com/peterbecker/xls-utils/tree/fat_jar
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。