我相信这个问题之前也被问过。我已经尝试过提到的其他选项,但没有成功:(
背景:使用 maven 打包一个包含所有依赖项的 jar。
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>assembly:package</id>
<phase>package</phase>
<goals>
<!-- Work around for http://jira.codehaus.org/browse/MASSEMBLY-97
as the goal should be attached. -->
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.Application</mainClass>
</manifest>
<manifestEntries>
<Class-Path>config/</Class-Path>
</manifestEntries>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
类路径
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java" />
<classpathentry kind="src" path="src/main/resources" />
<classpathentry exported="true" kind="con"
path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" />
<classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7" />
</classpath>
当我使用运行导出的 jar 时
java -cp Scheduler-0.0.1-SNAPSHOT-exe.jar com.Application
我得到了例外
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
ontext/support/ClassPathXmlApplicationContext
at com.Application.main(Application.java:13)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.support
.ClassPathXmlApplicationContext
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
对此有什么想法吗?我为所有依赖的 jar 使用 spring 版本 3.0.6。
谢谢 Gendaful