使用Maven导出.jar的NoClassDefFoundError

时间:2017-02-24 18:56:03

标签: java eclipse maven jar jetty

我正在尝试将服务器应用程序转换为Maven项目,并使用Maven将其导出为.jar。将项目导入eclipse并使用Maven插件转换后,我得到了一个POM文件。在运行mvn包之后,项目中存在许多涉及.jar库依赖项的错误。

我将这些依赖项添加到POM中,并添加了一个允许导出可运行的.jar文件的插件。我的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>160422_v3_Metadecoder</groupId>
  <artifactId>160422_v3_Metadecoder</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
 <plugin>
      <!-- Build an executable JAR -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>com.class.ws.classServer</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
    </plugins>
  </build>

  <dependencies>
  <dependency>
  <groupId>gson-2.6.2</groupId>
  <artifactId>gson-2.6.2</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\gson-2.6.2.jar</systemPath>
</dependency>
  <dependency>
  <groupId>classDecLib</groupId>
  <artifactId>classDecLib</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\classDecLib.jar</systemPath>
</dependency>
<dependency>
  <groupId>logging-interceptor-2.7.5</groupId>
  <artifactId>logging-interceptor-2.7.5</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\logging-interceptor-2.7.5.jar</systemPath>
</dependency>
<dependency>
  <groupId>javaClientAPI-1302</groupId>
  <artifactId>javaClientAPI-1302</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\javaClientAPI-1302.jar</systemPath>
</dependency>
<dependency>
  <groupId>javaClientAPI-2202</groupId>
  <artifactId>javaClientAPI-2202</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\javaClientAPI-2202.jar</systemPath>
</dependency>
<dependency>
  <groupId>joda-time-2.9.3</groupId>
  <artifactId>joda-time-2.9.3</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\joda-time-2.9.3.jar</systemPath>
</dependency>
<dependency>
  <groupId>okhttp-2.7.5</groupId>
  <artifactId>okhttp-2.7.5</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\okhttp-2.7.5.jar</systemPath>
</dependency>
<dependency>
  <groupId>swagger-annotations-1.5.9</groupId>
  <artifactId>swagger-annotations-1.5.9</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\swagger-annotations-1.5.9.jar</systemPath>
</dependency>
<dependency>
  <groupId>cdi-websocket-9.3.6.v20151106</groupId>
  <artifactId>cdi-websocket-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\cdi-websocket-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>servlet-api-3.1</groupId>
  <artifactId>servlet-api-3.1</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\servlet-api-3.1.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-xml-9.3.6.v20151106</groupId>
  <artifactId>jetty-xml-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-xml-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-webapp-9.3.6.v20151106</groupId>
  <artifactId>jetty-webapp-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-webapp-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-util-9.3.6.v20151106</groupId>
  <artifactId>jetty-util-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-util-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-servlet-9.3.6.v20151106</groupId>
  <artifactId>jetty-servlet-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-servlet-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-server-9.3.6.v20151106</groupId>
  <artifactId>jetty-server-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-server-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-security-9.3.6.v20151106</groupId>
  <artifactId>jetty-security-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-security-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-schemas-3.1</groupId>
  <artifactId>jetty-schemas-3.1</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-schemas-3.1.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-rewrite-9.3.6.v20151106</groupId>
  <artifactId>jetty-rewrite-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-rewrite-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-quickstart-9.3.6.v20151106</groupId>
  <artifactId>jetty-quickstart-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-quickstart-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-proxy-9.3.6.v20151106</groupId>
  <artifactId>jetty-proxy-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-proxy-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-plus-9.3.6.v20151106</groupId>
  <artifactId>jetty-plus-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-plus-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-nosql-9.3.6.v20151106</groupId>
  <artifactId>jetty-nosql-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-nosql-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-jndi-9.3.6.v20151106</groupId>
  <artifactId>jetty-jndi-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-jndi-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-jmx-9.3.6.v20151106</groupId>
  <artifactId>jetty-jmx-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-jmx-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-jaspi-9.3.6.v20151106</groupId>
  <artifactId>jetty-jaspi-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-jaspi-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-jaas-9.3.6.v20151106</groupId>
  <artifactId>jetty-jaas-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-jaas-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-io-9.3.6.v20151106</groupId>
  <artifactId>jetty-io-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-io-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-infinispan-9.3.6.v20151106</groupId>
  <artifactId>jetty-infinispan-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-infinispan-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-http-9.3.6.v20151106</groupId>
  <artifactId>jetty-http-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-http-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-deploy-9.3.6.v20151106</groupId>
  <artifactId>jetty-deploy-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-deploy-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-continuation-9.3.6.v20151106</groupId>
  <artifactId>jetty-continuation-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-continuation-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-client-9.3.6.v20151106</groupId>
  <artifactId>jetty-client-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-client-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-annotations-9.3.6.v20151106</groupId>
  <artifactId>jetty-annotations-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-annotations-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>jetty-alpn-server-9.3.6.v20151106</groupId>
  <artifactId>jetty-alpn-server-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\jetty-alpn-server-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>gcloud-session-manager-9.3.6.v20151106</groupId>
  <artifactId>gcloud-session-manager-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\gcloud-session-manager-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>cdi-servlet-9.3.6.v20151106</groupId>
  <artifactId>cdi-servlet-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\cdi-servlet-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>websocket-servlet-9.3.6.v20151106</groupId>
  <artifactId>websocket-servlet-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\websocket-servlet-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>websocket-server-9.3.6.v20151106</groupId>
  <artifactId>websocket-server-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\websocket-server-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>websocket-common-9.3.6.v20151106</groupId>
  <artifactId>websocket-common-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\websocket-common-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>websocket-client-9.3.6.v20151106</groupId>
  <artifactId>websocket-client-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\websocket-client-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>websocket-api-9.3.6.v20151106</groupId>
  <artifactId>websocket-api-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\websocket-api-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>javax.websocket-api-1.0</groupId>
  <artifactId>javax.websocket-api-1.0</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\javax.websocket-api-1.0.jar</systemPath>
</dependency>
<dependency>
  <groupId>javax-websocket-server-impl-9.3.6.v20151106</groupId>
  <artifactId>javax-websocket-server-impl-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\javax-websocket-server-impl-9.3.6.v20151106.jar</systemPath>
</dependency>
<dependency>
  <groupId>javax-websocket-client-impl-9.3.6.v20151106</groupId>
  <artifactId>javax-websocket-client-impl-9.3.6.v20151106</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}\lib\websocket\javax-websocket-client-impl-9.3.6.v20151106.jar</systemPath>
</dependency>
  </dependencies>
</project>

项目正确构建但是当我运行java -jar example.jar时,出现的.jar会出现以下错误:

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(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.server.Handler
        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)
        ... 7 more

对于可能导致这种情况的任何建议都会非常感激。

编辑:我将我的插件更改为:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <!-- get all project dependencies -->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!-- MainClass in mainfest make a executable jar -->
                    <archive>
                      <manifest>
                        <mainClass>com.class.ws.classServer</mainClass>
                      </manifest>
                    </archive>

                </configuration>
                <executions>
                  <execution>
                    <id>make-assembly</id>
                                        <!-- bind to the packaging phase -->
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>

尝试使用导出的JAR获取所有jar依赖项,但这会产生相同的输出。

1 个答案:

答案 0 :(得分:3)

在你的pom的构建部分,你需要以下插件来包含你的jar的类路径的依赖项,然后它们应该使用这个配置位于target / lib文件夹中):

              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

编辑:以下是该部分的pom应该是什么样子(当然您可以根据需要修改插件的设置):

<build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>org.my.main.class</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>