我尝试在我的 Ubuntu 机器上运行Spring 3 Hello World 教程,但遇到了一些问题。
首先,我正在开发 Springsource ToolSuite 3.1.0 版本,并在我的机器上安装了 Maven。我已经在同一个站点上浏览了一个带有 Maven 的 Java 应用教程。
首先,Maven 不应该解决依赖问题吗?当我写
import org.springframework.*;
我收到一条错误消息,指出无法找到该库,因此我必须手动将 JAR 本身添加到构建路径以解决该问题。尽管构建路径向导中存在许多 .m2/REPO 库路径。
mvn --version
返回
jason@asus:~/IDE/springsource/sts-3.1.0.RELEASE/plugins$ mvn --version
Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.6.0_24, vendor: Sun Microsystems Inc.
Java home: /usr/lib/jvm/java-6-openjdk-i386/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.5.7-03050702-generic", arch: "i386", family: "unix"
其次,当从命令行执行时,我得到这个打印输出:
jason@asus:~/Documents/workspace-sts-/SpringExample$ java -cp target/SpringExample-1.0-SNAPSHOT.jar com.jasonjohns.App
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/beans/factory/BeanFactory
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getMethod0(Class.java:2685)
at java.lang.Class.getMethod(Class.java:1620)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:492)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:484)
Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.BeanFactory
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
可以肯定的是,我检查了 STS/plugins 文件夹,那里有一个org.springframework.beans_3.1.1.RELEASE.jar
。
我的 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.jasonjohns</groupId>
<artifactId>SpringExample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringExample</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring 3 Dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
</dependencies>
</project>