我一直在为我的公司创建一个带有java的soap服务器,最近我转而使用Bone-CP和Maven来导入所有必需的第三方程序。完成bone-CP后,我使用了服务器命令
jar -cvfm SoapServer.jar manifest.txt SoapServer
当我将它转移到我的服务器并试图运行它时,我收到了这个错误:
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.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.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)
我认为导致问题的唯一想法是maven没有将所需的JAR打包到程序中?如果是这种情况,我只需要下载它们并将它们添加到类路径中吗?
答案 0 :(得分:14)
如果您需要将所有依赖项打包在可执行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>
上面的块会将lib /中的所有库添加到清单类路径条目
中的类路径中并将所有依赖项复制到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>