java 第一次使用maven后出现Java.lang.NoClassDefFoundError?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15840367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java.lang.NoClassDefFoundError after using maven for the first time?
提问by Kynian
I've been creating a soap server with java for my company and I just recently switched over to using Bone-CP and Maven to import all the required 3rd party programs. After I finished implementing bone-CP I used the server command
我一直在为我的公司用 java 创建一个肥皂服务器,我最近才切换到使用 Bone-CP 和 Maven 来导入所有必需的 3rd 方程序。完成骨骼CP后,我使用了服务器命令
jar -cvfm SoapServer.jar manifest.txt SoapServer
And when I transfered it to my server and tried to run it I got this error:
当我将它传输到我的服务器并尝试运行它时,我收到了这个错误:
Exception in thread "main" java.lang.NoClassDefFoundError: SoapServer/SoapServer (wrong name: com/test/SoapServer/SoapServer)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access0(URLClassLoader.java:71)
at java.net.URLClassLoader.run(URLClassLoader.java:361)
at java.net.URLClassLoader.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
The only think I can think of causing the problem is that maven doesn't package the required JARs with the program? If that's the case do I just need to download them and add them to the class path?
我能想到的唯一导致问题的想法是 maven 没有将所需的 JAR 与程序打包在一起?如果是这种情况,我是否只需要下载它们并将它们添加到类路径中?
回答by Jigar Joshi
If you need all of your dependencies to be packed in the executable jar then configure your pom like
如果您需要将所有依赖项打包在可执行 jar 中,请配置您的 pom,例如
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.something.YourMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
above block will add all the library in lib/ to classpath in manifest classpath entry
上面的块会将 lib/ 中的所有库添加到清单类路径条目中的类路径
and to copy all the dependencies to lib directory
并将所有依赖项复制到 lib 目录
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>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>