一、异常描述
关于在junit调试springmvc项目的时候报"java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing...Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing"异常错误,详情日志如下>>
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing@b@ at java.lang.ClassLoader.defineClass1(Native Method)@b@ at java.lang.ClassLoader.defineClass(ClassLoader.java:800)@b@ at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)@b@ at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)@b@ at java.net.URLClassLoader.access$100(URLClassLoader.java:71)@b@ at java.net.URLClassLoader$1.run(URLClassLoader.java:361)@b@ at java.net.URLClassLoader$1.run(URLClassLoader.java:355)@b@ at java.security.AccessController.doPrivileged(Native Method)@b@ at java.net.URLClassLoader.findClass(URLClassLoader.java:354)@b@ at java.lang.ClassLoader.loadClass(ClassLoader.java:425)@b@ at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)@b@ at java.lang.ClassLoader.loadClass(ClassLoader.java:358)@b@ at java.lang.Class.getDeclaredConstructors0(Native Method)@b@ at java.lang.Class.privateGetDeclaredConstructors(Class.java:2595)@b@ at java.lang.Class.getConstructor0(Class.java:2895)@b@ at java.lang.Class.getConstructor(Class.java:1731)@b@ at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)@b@ at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)@b@ at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)@b@ at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)@b@ at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)@b@ at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)@b@ at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)@b@ at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)@b@ at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)@b@ at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)@b@ at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)@b@ at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)@b@ at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)@b@ at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)@b@Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing@b@ at java.net.URLClassLoader$1.run(URLClassLoader.java:366)@b@ at java.net.URLClassLoader$1.run(URLClassLoader.java:355)@b@ at java.security.AccessController.doPrivileged(Native Method)@b@ at java.net.URLClassLoader.findClass(URLClassLoader.java:354)@b@ at java.lang.ClassLoader.loadClass(ClassLoader.java:425)@b@ at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)@b@ at java.lang.ClassLoader.loadClass(ClassLoader.java:358)@b@ ... 30 more
二、解决方法
修改pom.xml的将junit的依赖版本由3.8.1改为4.4,具体如下所示(完整的项目代码下载参见其他文章页)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">@b@ <modelVersion>4.0.0</modelVersion>@b@ <groupId>com.yiibai</groupId>@b@ <artifactId>HelloSpringMVC</artifactId>@b@ <packaging>war</packaging>@b@ <version>0.0.1-SNAPSHOT</version>@b@ <name>HelloSpringMVC Maven Webapp</name>@b@ <url>http://maven.apache.org</url>@b@ <dependencies>@b@@b@ <dependency>@b@ <groupId>junit</groupId>@b@ <artifactId>junit</artifactId>@b@ <version>4.4</version>@b@ <scope>test</scope>@b@ </dependency>@b@@b@ <!-- Servlet Library -->@b@ <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->@b@ <dependency>@b@ <groupId>javax.servlet</groupId>@b@ <artifactId>javax.servlet-api</artifactId>@b@ <version>3.1.0</version>@b@ <scope>provided</scope>@b@ </dependency>@b@@b@ <!-- Spring dependencies -->@b@ <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->@b@ <dependency>@b@ <groupId>org.springframework</groupId>@b@ <artifactId>spring-core</artifactId>@b@ <version>4.1.4.RELEASE</version>@b@ </dependency>@b@@b@ <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->@b@ <dependency>@b@ <groupId>org.springframework</groupId>@b@ <artifactId>spring-web</artifactId>@b@ <version>4.1.4.RELEASE</version>@b@ </dependency>@b@@b@ <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->@b@ <dependency>@b@ <groupId>org.springframework</groupId>@b@ <artifactId>spring-webmvc</artifactId>@b@ <version>4.1.4.RELEASE</version>@b@ </dependency>@b@ @b@ <dependency>@b@ <groupId>org.springframework</groupId>@b@ <artifactId>spring-test</artifactId>@b@ <version>4.1.4.RELEASE</version>@b@ </dependency>@b@ @b@ @b@ <dependency>@b@ <groupId>org.springframework</groupId>@b@ <artifactId>spring</artifactId>@b@ <version>2.5.6.SEC03</version>@b@ </dependency>@b@ @b@ <dependency>@b@ <groupId>freemarker</groupId>@b@ <artifactId>freemarker</artifactId>@b@ <version>2.3.21</version>@b@ </dependency>@b@ @b@ <dependency>@b@ <groupId>org.slf4j</groupId>@b@ <artifactId>slf4j-api</artifactId>@b@ <version>1.6.1</version>@b@ </dependency>@b@@b@ </dependencies>@b@@b@ <build>@b@ <finalName>HelloSpringMVC</finalName>@b@ <plugins>@b@ @b@ <plugin>@b@ <groupId>org.apache.tomcat.maven</groupId>@b@ <artifactId>tomcat7-maven-plugin</artifactId>@b@ <version>2.2</version> @b@ </plugin>@b@ </plugins>@b@ </build>@b@</project>