[英]NoClassDefFoundError when compiling Spring project in terminal
我正在嘗試學習 Spring,並且正在學習本教程:
https://www.javatpoint.com/spring-tutorial 。
這是我所做的:
我創建了一個目錄springtutorial
並將這兩個文件放入其中:
Student.java
package com.javatpoint;
public class Student{
private String name;
public String getName(){
return name;
}
public void displayInfo(){
System.out.println("Hello:" + name);
}
}
Test.java
:
package com.javatpoint;
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 Test {
public static void main(String[] args){
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
Student student = (Student) factory.getBean("studentbean");
student.displayInfo();
}
}
另外,我創建了一個 XML 文件applicationContext.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="studentbean" class="com.javatpoint.Student">
<propertyname="name" value="Vimal Jaiswal"></property>
</bean>
</beans>
最后,我下載了這些 Spring 核心文件: https : //www.javatpoint.com/src/sp/spcorejars.zip
我把除了 .jar 文件之外的所有東西都放在同一個文件夾中。 .jar 文件放置在一個子文件夾中:
~/springtutorial$ ls
applicationContext.xml spcorejars Student.java Test.java
~/springtutorial$ ls spcorejars/
com.springsource.org.apache.commons.logging-1.1.1.jar org.springframework.beans-3.0.1.RELEASE-A.jar
com.springsource.org.apache.log4j-1.2.15.jar org.springframework.context-3.0.1.RELEASE-A.jar
jmxtools-1.2.1.jar org.springframework.core-3.0.1.RELEASE-A.jar
org.springframework.asm-3.0.1.RELEASE-A.jar org.springframework.expression-3.0.1.RELEASE-A.jar
編譯Student.java
工作正常:
~/springtutorial$ javac -d . Student.java
經過一番修修補補,我設法編譯了Test.java
(單獨的子文件夾中的 .jar 文件):
~/springtutorial$ javac -cp ".:spcorejars/*" -d . Test.java
但我無法運行測試文件:
~/springtutorial$ java com.javatpoint.Test
Error: Unable to initialize main class com.javatpoint.Test
Caused by: java.lang.NoClassDefFoundError: org/springframework/core/io/Resource
通過在線查看,在我看來我應該擁有我需要的所有 .jar 文件。 那么這個錯誤的原因可能是什么?
在你問之前:不,我不想使用 Maven 或 Gradle 來做這件事。 這樣做的目的是在沒有任何程序為您完成的情況下查看一切實際工作方式。
嘗試運行命令:
~/springtutorial$ java -cp ".:spcorejars/*" com.javatpoint.Test
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.