Reputation: 3984
I have been stuck for 12 hours straight now on this error, tried every possible way . My dependencies are right
<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.hello</groupId>
<artifactId>helloworld</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>helloworld</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
SpringBeans.xml file
<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-4.3.xsd">
<bean id="helloBean" class="com.hello.HelloWorld">
<property name="name" value="world" />
</bean>
But I get this ---->
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:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException:
org.springframework.beans.factory.BeanFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:359)
at java.net.URLClassLoader$1.run(URLClassLoader.java:348)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:347)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:312)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
I compile with maven and it builds fine but when I run it with java and necessary classpaths I get the above error. No clue what could be going wrong, tried googling for hours but no luck.
This is the command line I am using to run :
java -cp ".:target/helloworld-1.0-SNAPSHOT.jar:lib/*" com.hello.App
where lib directory has spring-context and spring-core jar files
Upvotes: 0
Views: 86
Reputation: 86754
When using Maven, it will automatically download transitive dependencies of all the libraries you explicitly specify in your POM. This means the list of actual dependencies may (probably will) be quite a bit larger than what your POM contains, and all of these dependencies are required to run your application.
If you want to list the complete set of dependencies use the command
mvn dependency:tree
However, you shouldn't need to worry about any of this if you stay completely within Maven. For example, to run your compiled project (assuming it can be run from the command line), just use mvn exec:java
. If you need to package a .jar
with dependencies there are options to do that as well.
You need the list of dependencies only if you're going to try to run your code outside of Maven, such as directly from the command line with the java
command.
Upvotes: 1
Reputation: 7938
You need spring-beans
jar in your runtime class path. org.springframework.beans.factory.BeanFactory
class is in this jar. I suggest handling runtime dependencies by creating fatJar or using maven dependency plugin to collect dependencies to a folder and adding this folder to runtime class path.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
How can I create an executable JAR with dependencies using Maven?
https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html
Upvotes: 1