【发布时间】:2018-10-06 03:40:17
【问题描述】:
我对 Spring 和 maven 都很陌生。我收到一个错误。我正在从命令行使用 Maven 来构建项目。
我已经更新了 spring-core 和 spring-context 依赖项,之后我完成了mvn clean install,它说没问题。我在它之后做了mvn eclipse:eclipse,它也很顺利。我用mvn clean compile 编译,也通过了。我已经检查了 .m2 文件夹(在 MacOS 上),是的 spring-core 在那里。然而,在运行我的项目时,这个运行时异常即将到来;我尝试了 Stack Overflow 上给出的其他几种解决方案,但没有一个有效。
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/io/Resource
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.springframework.core.io.Resource
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
这是我的 POM 文件。
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Spring_1</groupId>
<artifactId>SpringCIList</artifactId>
<version>1.0-SNAPSHOT</version>
<name>SpringCIList</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我的一个小疑问,这可以吗(beans -2.5 xsd)
<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">
成为问题的根源??
编辑 1 导致异常发生的确切运行命令
.
Rishis-MBP:SpringCIList rishiprakash$ cd target/classes/
Rishis-MBP:classes rishiprakash$ ls
Spring_1 spring-module.xml
Rishis-MBP:classes rishiprakash$ java Spring_1.App
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/io/Resource
at java.lang.Class.getDeclaredMethods0(Native Method)......
App.java
package Spring_1;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class App
{
public static void main( String[] args )
{
Resource r = new ClassPathResource("spring-module.xml");
BeanFactory factory = new XmlBeanFactory(r);
Employee e = (Employee)factory.getBean("emp");
System.out.println( "Hello World!" );
e.displayInfo();
}
}
Employee.java
package Spring_1;
class Employee{
private String name;
String getName(){
return this.name;
}
void setName(String name){
this.name = name;
}
void displayInfo(){
System.out.println("name of employee is"+this.name);
}
}
spring-module.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="emp" class="Spring_1.Employee">
<property name="name" value="Rishi"></property>
</bean>
</beans>
导入到 Eclipse 时的相同项目(使用 m2e 插件)正在运行。
【问题讨论】:
标签: java spring maven spring-mvc maven-2