因爲我的Maven應用程序有一些奇怪的問題,所以我決定創建一個用Maven管理的最簡單的Spring獨立Java應用程序,以查明我的問題在哪裏。我附上我的所有代碼,代碼是最簡單的,但有些錯誤。我必須失明,但你看到什麼可能導致這個異常?簡單的Spring和Maven應用程序 - java.lang.NoClassDefFoundError異常
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2531)
at java.lang.Class.getMethod0(Class.java:2774)
at java.lang.Class.getMethod(Class.java:1663)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
我的系統設置:
macbook:Notification jirka$ env
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/bash
TERM=xterm-256color
TMPDIR=/var/folders/tx/c11w5sf11zj6qbfh5skp8kx00000gn/T/
Apple_PubSub_Socket_Render=/tmp/launch-jFadXh/Render
TERM_PROGRAM_VERSION=326
TERM_SESSION_ID=827724B1-6606-411F-B9A4-8649BF09D455
USER=jirka
SSH_AUTH_SOCK=/tmp/launch-0SCequ/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:29:56
PATH=/Users/jirka/java/apache-maven-3.1.1/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin
__CHECKFIX1436934=1
PWD=/Users/jirka/Documents/workspace/sandbox/Notification
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
LANG=cs_CZ.UTF-8
M2_HOME=/Users/jirka/java/apache-maven-3.1.1
SHLVL=1
HOME=/Users/jirka
LOGNAME=jirka
_=/usr/bin/env
OLDPWD=/Users/jirka/Documents/workspace/sandbox
什麼沒有設置爲CLASS_PATH
,但是這是我的應用程序的根目錄.classpath
文件的內容 - 我不知道,如果缺少CLASS_PATH
不是問題。如果是這樣,那麼CLASS_PATH
應該指向OS X和Maven 3?
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
</classpath>
我創建Maven項目:
mvn archetype:generate -DgroupId=com.example -DartifactId=Sandbox -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
我設置的依賴關係到
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>Sandbox</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Notification</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
然後,我創建了簡單的Spring應用pom.xml
配置文件...
內容applicationContext.xml
文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="helloBean" class="com.example.HelloWorld">
<property name="name" value="Jirka" />
</bean>
</beans>
和HelloWorld.java
:
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void printHello() {
System.out.println("Hello ! " + name);
}
}
和App.java:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloBean");
obj.printHello();
}
}
然後我打包應用程序到jar文件:
mvn package
和tryied到運行它:
java -cp target/Notification-1.0-SNAPSHOT.jar com.example.App
但這種錯誤發生:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2531)
at java.lang.Class.getMethod0(Class.java:2774)
at java.lang.Class.getMethod(Class.java:1663)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
請閱讀此[帖子](http:// stackoverflow。com/questions/574594/how-can-i-create-an-executable-jar -with-dependencies-using-maven)也許它會幫助你。 – kreats