Spring java.lang.NoClassDefFoundError error

huangapple go评论67阅读模式
英文:

Spring java.lang.NoClassDefFoundError error

问题

我是Spring框架的新手,正在按照tutorialspoint.com上的教程学习。在包含所有内容后,当我运行程序时,它给了我以下错误:
> 错误:无法初始化主类com.raza.spring.MainApp
> 引发的错误:java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

以下是我的项目结构:
点击此处查看图片描述

以下是我的代码:

Hello类:

package com.raza.spring;
public class Hello {

	private String message;
	
	public void setMessage(String message) {
		this.message = message;
	}
	
	public String getMessage() {
		return this.message;
	}
}

MainApp类:

package com.raza.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
		Hello obj = (Hello) context.getBean("helloWorld");
		
		obj.getMessage();
	}

}

Beans.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

	<bean id="helloWorld" class="com.raza.spring.Hello">
		<property name="message" value="Hello raza world !!!"/>
	</bean>

</beans>

我无法弄清楚问题出在哪里。提前感谢您的帮助。

我正在尝试tutorialspoint.com上的教程,并期望在控制台上看到输出。

英文:

I am new to spring framework and following the tutorial on tutorialspoint.com. After including every thing, while running the program it gives me following error:
> Error: Unable to initialize main class com.raza.spring.MainApp
> Caused by: java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

Below is my project structure:
enter image description here

Here is the code:

Class Hello:

package com.raza.spring;
public class Hello {

	private String message;
	
	public void setMessage(String message) {
		this.message = message;
	}
	
	public String getMessage() {
		return this.message;
	}
}

Class MainApp:

package com.raza.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ApplicationContext context = new ClassPathXmlApplicationContext(&quot;Beans.xml&quot;);
		Hello obj = (Hello) context.getBean(&quot;helloWorld&quot;);
		
		obj.getMessage();
	}

}

Beans.xml

&lt;?xml version = &quot;1.0&quot; encoding = &quot;UTF-8&quot;?&gt;

&lt;beans xmlns = &quot;http://www.springframework.org/schema/beans&quot;
   xmlns:xsi = &quot;http://www.w3.org/2001/XMLSchema-instance&quot;
   xsi:schemaLocation = &quot;http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	&lt;bean id = &quot;helloWorld&quot; class = &quot;com.raza.spring.Hello&quot;&gt;
		&lt;property name = &quot;message&quot; value = &quot;Hello raza world !!!&quot;/&gt;
	&lt;/bean&gt;

&lt;/beans&gt;

Not able to get what is wrong here. Thanks in advance.

I am trying tutorial on tutorialspoint.com and expecting an output on the console.

答案1

得分: 1

java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext 意味着你的代码中使用的类在你的项目中不可访问。
在之前的章节 https://www.tutorialspoint.com/spring/spring_environment_setup.htm 中,你可能在第4步下载了Spring库。尝试将其添加到你的类路径中:https://wiki.eclipse.org/FAQ_How_do_I_add_an_extra_library_to_my_project%27s_classpath%3F。

我强烈建议你开始使用Maven。它使你能够更轻松地添加外部库,而无需手动从Git仓库或网站下载它们。
同时,请查看Baeldung的课程。他们的示例始终使用最新的Spring版本:https://www.baeldung.com/spring-tutorial

英文:

java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext tells that the Class you are using in your code is not accessible from your project.
In previous chapter on https://www.tutorialspoint.com/spring/spring_environment_setup.htm you probably downloaded spring library in step 4. Try adding it to your classpath: https://wiki.eclipse.org/FAQ_How_do_I_add_an_extra_library_to_my_project%27s_classpath%3F.

I highly recommend you start using maven. It allows you to add external libraries a lot easier, without having to manually download them from a git repo or a website.
Also, check the Baeldung courses. Their examples always use the latest Spring version https://www.baeldung.com/spring-tutorial

huangapple
  • 本文由 发表于 2023年2月8日 12:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定