【问题标题】:NoClassDefFoundError when running .jar file运行 .jar 文件时出现 NoClassDefFoundError
【发布时间】:2019-01-20 01:31:31
【问题描述】:

我开发了一个简单的应用程序,它带有一个使用 Jersey 的嵌入式 Jetty 服务器。

当我在 Eclipse 上运行应用程序时一切正常,但是当我使用 mvn clean compile package 生成 .jar 文件然后尝试通过执行 java -jar PATH_TO_THE_JAR_FILE 在命令行上运行可执行文件时,我得到以下堆栈跟踪:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/server/Handler
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.server.Handler
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

我的 pom:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.myserver</groupId>
<artifactId>lineserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>lineserver</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.salsify.lineserver.server.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server -->
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.27</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet-core -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.27</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-jetty-http -->
    <!-- <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
        <version>2.27</version>
    </dependency>-->
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.27</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-server -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.4.12.v20180830</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-servlet -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.4.12.v20180830</version>
    </dependency>
    <!-- <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-util</artifactId>
        <version>9.4.14.v20181114</version>
    </dependency>-->
    <!-- https://mvnrepository.com/artifact/com.google.inject/guice -->
    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>4.2.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.inject.extensions/guice-servlet -->
    <dependency>
        <groupId>com.google.inject.extensions</groupId>
        <artifactId>guice-servlet</artifactId>
        <version>4.2.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-guice -->
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-guice</artifactId>
        <version>1.19.4</version>
    </dependency>

</dependencies>

依赖之间没有冲突,运行mvn dependency:tree我明白了:

[INFO] +- junit:junit:jar:3.8.1:test
[INFO] +- org.glassfish.jersey.core:jersey-server:jar:2.27:compile
[INFO] |  +- org.glassfish.jersey.core:jersey-common:jar:2.27:compile
[INFO] |  |  \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile
[INFO] |  +- org.glassfish.jersey.core:jersey-client:jar:2.27:compile
[INFO] |  +- javax.ws.rs:javax.ws.rs-api:jar:2.1:compile
[INFO] |  +- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.27:compile
[INFO] |  +- javax.annotation:javax.annotation-api:jar:1.2:compile
[INFO] |  +- org.glassfish.hk2.external:javax.inject:jar:2.5.0-b42:compile
[INFO] |  \- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] +- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.27:compile
[INFO] +- org.glassfish.jersey.inject:jersey-hk2:jar:2.27:compile
[INFO] |  \- org.glassfish.hk2:hk2-locator:jar:2.5.0-b42:compile
[INFO] |     +- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.5.0-b42:compile
[INFO] |     +- org.glassfish.hk2:hk2-api:jar:2.5.0-b42:compile
[INFO] |     +- org.glassfish.hk2:hk2-utils:jar:2.5.0-b42:compile
[INFO] |     \- org.javassist:javassist:jar:3.22.0-CR2:compile
[INFO] +- org.eclipse.jetty:jetty-server:jar:9.4.14.v20181114:compile
[INFO] |  +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] |  +- org.eclipse.jetty:jetty-http:jar:9.4.14.v20181114:compile
[INFO] |  |  \- org.eclipse.jetty:jetty-util:jar:9.4.14.v20181114:compile
[INFO] |  \- org.eclipse.jetty:jetty-io:jar:9.4.14.v20181114:compile
[INFO] +- org.eclipse.jetty:jetty-servlet:jar:9.4.14.v20181114:compile
[INFO] |  \- org.eclipse.jetty:jetty-security:jar:9.4.14.v20181114:compile
[INFO] +- com.google.inject:guice:jar:4.2.2:compile
[INFO] |  +- javax.inject:javax.inject:jar:1:compile
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  \- com.google.guava:guava:jar:25.1-android:compile
[INFO] |     +- com.google.code.findbugs:jsr305:jar:3.0.2:compile
[INFO] |     +- org.checkerframework:checker-compat-qual:jar:2.0.0:compile
[INFO] |     +- com.google.errorprone:error_prone_annotations:jar:2.1.3:compile
[INFO] |     +- com.google.j2objc:j2objc-annotations:jar:1.1:compile
[INFO] |     \- org.codehaus.mojo:animal-sniffer-annotations:jar:1.14:compile
[INFO] +- com.google.inject.extensions:guice-servlet:jar:4.2.2:compile
[INFO] \- com.sun.jersey.contribs:jersey-guice:jar:1.19.4:compile
[INFO]    \- com.sun.jersey:jersey-servlet:jar:1.19.4:compile
[INFO]       \- com.sun.jersey:jersey-server:jar:1.19.4:compile
[INFO]          \- com.sun.jersey:jersey-core:jar:1.19.4:compile
[INFO]             \- javax.ws.rs:jsr311-api:jar:1.1.1:compile

【问题讨论】:

  • 您是否检查了库是否被打包在最终的 jar 中?
  • @ngueno 他们确实没有被打包。但是,当我用 eclipse 构建一个 jar 时,它们被打包了。我正在运行 jar -tf jarfile 来检查内容...是否与 maven 打包过程有关

标签: java eclipse maven embedded-jetty


【解决方案1】:

要将具有依赖项的类打包到可执行 jar 中,您应该使用 maven-shade-plugin:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>com.salsify.lineserver.server.Main</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>

您可以从 pom 中删除 maven-jar-plugin 配置。

【讨论】:

  • maven-assembly-pluginjar-with-dependencies 配置为 another choice
  • 我推荐maven-shade-plugin 而不是maven-assembly-plugin,因为它似乎得到了更好的维护和更频繁的发布。版本3.1.0 的程序集插件有一个错误,导致它需要花费 2-3 分钟来创建我们的一些 jar。我不知道这是否在最新的3.1.1 中得到修复,但我仍然会选择 shade-plugin。
  • maven-shade-pluginmaven-assembly-plugin 带来的唯一好处是资源转换器(例如合并META-INF/services/ 文件)。 Eclipse Jetty 项目本身根据生成的工件的角色/目的使用程序集和遮蔽插件。 (公共 == 大会,私人 == 阴影)
  • @JoakimErdfelt 我已经提到构建时间是一个优势。很可能是由于组装插件中的错误,它为某些或我们的模块增加了 2-3 分钟的构建时间。当您需要嵌入时,Shade-plugin 还可以轻松过滤掉签名文件,例如bouncycastle 工件,如果包含,这将使签名检查在启动时失败。为什么工件是私有的还是公共的对插件的选择很重要?你能指出选择组件而不是阴影的具体优势吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 2021-09-28
  • 2013-04-26
  • 2012-05-01
  • 1970-01-01
相关资源
最近更新 更多