简体   繁体   English

运行时中的 Gradle Uber (Fat) Jar 和 java.lang.NoClassDefFoundError

[英]Gradle Uber (Fat) Jar and java.lang.NoClassDefFoundError in Runtime

I am trying to create a UberJar file with Gradle.我正在尝试使用 Gradle 创建一个 UberJar 文件。

To build and run the jar file I execute commands ./gradlew clean build java -jar build/libs/jLocalCoin-0.2.jar要构建和运行 jar 文件,我执行命令./gradlew clean build java -jar build/libs/jLocalCoin-0.2.jar

And I get Exception我得到异常

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/primitives/Longs
    // ... rest of stacktrace
    at im.djm.zMain.Main03.main(Main03.java:13)
Caused by: java.lang.ClassNotFoundException: com.google.common.primitives.Longs
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 8 more

I don't understand what is the reason for the exception?我不明白异常的原因是什么?
I do understand that Guava library is not part of the jar, but why?我知道 Guava 库不是 jar 的一部分,但为什么呢?

In this questio , in the accepted answer, the jar file is created in the same way as I try to do.在这种questio ,在接受的答案,以相同的方式,我尝试做创建的jar文件。

This is build.grade file这是build.grade文件

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

mainClassName = 'im.djm.zMain.Main03'

archivesBaseName = 'jLocalCoin'
version = "0.2"

run {
    standardInput = System.in
}

repositories {
    jcenter()
}

dependencies {
    implementation 'com.google.guava:guava:24.0-jre'

    testImplementation 'junit:junit:4.12'

    testCompile 'org.assertj:assertj-core:3.8.0'
}

jar {
  manifest { 
    attributes "Main-Class": "$mainClassName"
  }  

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

The jar you created probably does not have guava in it.您创建的罐子里可能没有番石榴。 NoClassDefFoundError occurs when your project does not have required runtime dependecies in the classpath.当您的项目在类路径中没有必需的运行时依赖项时,会发生NoClassDefFoundError The tutorial I checked created fatJar this way,我查的教程就是这样创建fatJar的,

task fatJar(type: Jar) {
   baseName = project.name + '-all'
   manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',
            'Implementation-Version': version,
            'Main-Class': 'com.mkyong.DateUtils'
    }
   from { configurations.compile.collect { it.isDirectory() ? it : 
    zipTree(it) } 
    }
   with jar
}

and change implementation 'com.google.guava:guava:24.0-jre' to compile 'com.google.guava:guava:24.0-jre' then it does include guava in the generated jar并更改implementation 'com.google.guava:guava:24.0-jre'compile 'com.google.guava:guava:24.0-jre'然后它确实在生成的 jar 中包含了番石榴

and run command gradle fatJar并运行命令gradle fatJar

source = https://www.mkyong.com/gradle/gradle-create-a-jar-file-with-dependencies/来源 = https://www.mkyong.com/gradle/gradle-create-a-jar-file-with-dependencies/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM