![](/img/trans.png)
[英]Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/core/io/Resource - using spring-core.4_2_5
[英]java.lang.NoClassDefFoundError: org/springframework/core/io/Resource Exception
我對 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)......
應用程序.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();
}
}
雇員.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 插件)時的相同項目正在運行。
您正在嘗試僅使用 .class 文件運行整個應用程序,而忘記告訴 JVM 依賴項。
可以使用以下方法解決以下問題: mvn exec:java -Dexec.mainClass="Spring_1.App"
但是我強烈建議構建 jar 文件或從您的 IDE (Eclipse, Idea) 執行它
我認為通過以這種方式運行您的應用程序,類路徑不包含所有依賴項。
請嘗試以下操作:
mvn clean package
(這應該在你的 target/ 文件夾中創建一個 jar 文件)jara -jar your_app_name.jar
發布是 spring-web 只能在 spring.core 4.2 版本之后工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.