I’ve tried running the Spring 3 Hello World tutorial on my Ubuntu machine and have run into a few issues.
First, I’m working on the Springsource ToolSuite 3.1.0 release, and have Maven installed on my machine. I already went thru a Java App with Maven tutorial on the same site.
First, isn’t Maven supposed to resolve dependency issues? When I wrote
import org.springframework.*;
I got an error stating that the library was not able to be found, so I had to add the JAR itself manually to the build path to solve that issue. This was despite many .m2/REPO library paths existing on the build path wizard.
mvn --version returns
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"
Secondly, when executing from the command line, I get this printout:
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)
Just to be sure, I checked the STS/plugins folder and there is an org.springframework.beans_3.1.1.RELEASE.jar there.
My pom.xml is
<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>
I’d try navigating to your project’s location in a terminal and then do
mvn eclipse:eclipse. This will (re)generate Eclipse/Spring Tool Suite-specific files – essentially creating .classpath and .project files (see http://maven.apache.org/plugins/maven-eclipse-plugin/eclipse-mojo.html).The .classpath is essentially your build path and by running eclipse:eclipse maven will generate it for you based on the contents of your pom.xml.
Once done, refresh the project in Eclipse/STS. Making sure build automatically is ticked (Project->Build Automatically).