简体   繁体   English

在终端中编译 Spring 项目时出现 NoClassDefFoundError

[英]NoClassDefFoundError when compiling Spring project in terminal

I'm trying to learn Spring, and I'm following this tutorial:我正在尝试学习 Spring,并且正在学习本教程:

https://www.javatpoint.com/spring-tutorial . https://www.javatpoint.com/spring-tutorial

Here's what I've done:这是我所做的:

I've created a directory springtutorial and put these two files in it:我创建了一个目录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 : 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();
        }
}

Also, I've created an XML file applicationContext.xml :另外,我创建了一个 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>

Lastly, I've downloaded these Spring core files: https://www.javatpoint.com/src/sp/spcorejars.zip最后,我下载了这些 Spring 核心文件: https : //www.javatpoint.com/src/sp/spcorejars.zip

I've put everything except the .jar files in the same folder.我把除了 .jar 文件之外的所有东西都放在同一个文件夹中。 The .jar files are placed in a sub-folder: .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

Compiling Student.java works fine:编译Student.java工作正常:

~/springtutorial$ javac -d . Student.java

and after som tinkering I managed to compile Test.java (.jar files in separate sub-folder):经过一番修修补补,我设法编译了Test.java (单独的子文件夹中的 .jar 文件):

~/springtutorial$ javac -cp ".:spcorejars/*" -d . Test.java

But I can't run the test file:但我无法运行测试文件:

~/springtutorial$ java com.javatpoint.Test 
Error: Unable to initialize main class com.javatpoint.Test
Caused by: java.lang.NoClassDefFoundError: org/springframework/core/io/Resource

By looking online, it seems to me like I should have all the .jar files I need.通过在线查看,在我看来我应该拥有我需要的所有 .jar 文件。 So what could be the cause of this error?那么这个错误的原因可能是什么?

And before you ask: No, I don't want to use Maven or Gradle for doing this.在你问之前:不,我不想使用 Maven 或 Gradle 来做这件事。 The purpose of doing this is to see how everything actually works without any program doing it for you.这样做的目的是在没有任何程序为您完成的情况下查看一切实际工作方式。

尝试运行命令:

~/springtutorial$ java -cp ".:spcorejars/*" com.javatpoint.Test 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM