Java NoClassDefFoundError使用Maven导出.jar
我正在尝试将服务器应用程序转换为Maven项目,并使用Maven将其导出为.jar。将项目导入eclipse并使用Maven插件进行转换后,我得到了一个POM文件。运行mvn包后,出现了许多错误,涉及项目中的.jar库依赖项 我将这些依赖项添加到POM中,并添加了一个允许导出runnable.jar文件的插件。我的POM文件如下:Java NoClassDefFoundError使用Maven导出.jar,java,eclipse,maven,jar,jetty,Java,Eclipse,Maven,Jar,Jetty,我正在尝试将服务器应用程序转换为Maven项目,并使用Maven将其导出为.jar。将项目导入eclipse并使用Maven插件进行转换后,我得到了一个POM文件。运行mvn包后,出现了许多错误,涉及项目中的.jar库依赖项 我将这些依赖项添加到POM中,并添加了一个允许导出runnable.jar文件的插件。我的POM文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/
<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>
如果您对可能导致这种情况的原因有任何建议,我们将不胜感激
编辑:我将插件更改为:
<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>
org.apache.maven.plugins
maven汇编插件
2.4.1
带有依赖项的jar
com.class.ws.classServer
组装
包裹
单一的
在尝试使用导出的jar获取所有jar依赖项时,这会产生相同的输出。在pom的构建部分,您需要以下插件将依赖项包含到jar的类路径中,然后它们应该位于具有此配置的target/lib文件夹中):
org.apache.maven.plugins
maven依赖插件
复制依赖项
准备包装
复制依赖项
${project.build.directory}/lib
假的
假的
真的
编辑:下面是该部分pom的外观(当然您可以根据需要修改插件的设置):
org.apache.maven.plugins
maven jar插件
3.0.2
真的
解放党/
org.my.main.class
org.apache.maven.plugins
maven依赖插件
复制依赖项
准备包装
复制依赖项
${project.build.directory}/lib
假的
假的
真的
这是因为JAR只包含您的代码,没有任何依赖项。你需要这样做。这看起来很有希望,谢谢。。你能举个例子说明怎么做吗?这只是一个run命令,还是我需要将它们作为插件添加到POM中?我看到我需要指定每个jar文件的汇编POM插件,这将是不可管理的。从项目中提取这些jar似乎很愚蠢。这就是中央Maven回购协议的目的。重点应该是管理源代码之外的依赖项和版本。@duffymo那么,这里的合理选择是什么呢?创建额外的maven回购协议?我将项目转换为Maven的原因是,我可以在Visual Studio Online上构建它,然后对其进行管理并轻松部署到azure。可能想查看有关此主题的旧答案的结尾(它有两个用于实现此最终结果的不同方法的示例项目):当您解压缩jar时,你的清单文件里有什么?通常情况下,您应该获得主类和正确的大类路径?清单版本:1.0 Archiver版本:Plexus Archiver构建者:JakeD创建者:ApacheMaven 3.3.9构建者Jdk:1.8.0121。。。。看起来它没有得到类。看我的编辑,它对我来说很好,我从MANIFEST.MF:MANIFEST Version:1.0 builded By:ado class Path:lib/jsoup-1.10.2.jar lib/log4j-api-2.8.jar lib/log4j core-2.8.jar创建人:apachemaven 3.3.9 Build Jdk:1.8.0121main class:org.my.Main.classNow它说无法加载主类。。。但是清单确实有正确的类路径。我基本上是在编辑中使用了您的部分,修改了类名并删除了classpathPrefix。在删除此项之前不起作用。您应该让classpathPrefix指向您的依赖项应该复制到的目录(即target/lib),并且您需要它们来运行程序。您能用MANIFEST.MF中的内容、新的pom.xml以及target/lib目录中的内容列表编辑您的原始帖子吗?
<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>
<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>
<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>