gpt4 book ai didi

java - 简单的 Spring 和 Maven 应用程序 - java.lang.NoClassDefFoundError 异常

转载 作者:行者123 更新时间:2023-12-01 13:50:23 25 4
gpt4 key购买 nike

因为我的 Maven 应用程序遇到了一些奇怪的问题,所以我决定创建一个使用 Maven 管理的最简单的 Spring 独立 Java 应用程序,以找出问题所在。我附上了我的所有代码,该代码是最简单的,但有些问题。我一定是瞎了,但是你知道是什么导致了这个异常吗?

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2531)
at java.lang.Class.getMethod0(Class.java:2774)
at java.lang.Class.getMethod(Class.java:1663)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
<小时/>

我的系统设置:

macbook:Notification jirka$ env
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/bash
TERM=xterm-256color
TMPDIR=/var/folders/tx/c11w5sf11zj6qbfh5skp8kx00000gn/T/
Apple_PubSub_Socket_Render=/tmp/launch-jFadXh/Render
TERM_PROGRAM_VERSION=326
TERM_SESSION_ID=827724B1-6606-411F-B9A4-8649BF09D455
USER=jirka
SSH_AUTH_SOCK=/tmp/launch-0SCequ/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:29:56
PATH=/Users/jirka/java/apache-maven-3.1.1/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin
__CHECKFIX1436934=1
PWD=/Users/jirka/Documents/workspace/sandbox/Notification
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
LANG=cs_CZ.UTF-8
M2_HOME=/Users/jirka/java/apache-maven-3.1.1
SHLVL=1
HOME=/Users/jirka
LOGNAME=jirka
_=/usr/bin/env
OLDPWD=/Users/jirka/Documents/workspace/sandbox

未设置的是CLASS_PATH,但这是我的应用程序根目录中.classpath文件的内容 - 我不确定是否缺少 >CLASS_PATH 不是问题。如果是这样,CLASS_PATH 在 OS X 和 Maven 3 上应该指向哪里?

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
</classpath>
<小时/>

我创建了 Maven 项目:

mvn archetype:generate -DgroupId=com.example -DartifactId=Sandbox -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

我将依赖项设置到 pom.xml 配置文件

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>Sandbox</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Notification</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

然后我创建了简单的 Spring 应用程序...

applicationContext.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="helloBean" class="com.example.HelloWorld">
<property name="name" value="Jirka" />
</bean>

</beans>

HelloWorld.java:

public class HelloWorld {
private String name;

public void setName(String name) {
this.name = name;
}

public void printHello() {
System.out.println("Hello ! " + name);
}
}

和App.java:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloBean");
obj.printHello();
}
}

然后我将应用程序打包成jar文件:

mvn package

并尝试运行它:

java -cp target/Notification-1.0-SNAPSHOT.jar com.example.App

但是发生了这个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2531)
at java.lang.Class.getMethod0(Class.java:2774)
at java.lang.Class.getMethod(Class.java:1663)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more

最佳答案

您的 Maven 设置允许您编译代码并构建 jar,但 Spring 不在运行时类路径中。在命令行中将 Spring 添加到 -cp 中。

或者使用 Maven 程序集插件构建一个包含您的代码和所有依赖项的“uber”jar,然后运行它。

关于java - 简单的 Spring 和 Maven 应用程序 - java.lang.NoClassDefFoundError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20007777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com