0

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

0decimal0
  • 3,884
  • 2
  • 24
  • 39
  • 1
    _"and necessary classpaths "_ -- clearly you're missing something. For us to help you need to show the command line you use to run it that fails. – Jim Garrison Mar 24 '18 at 19:48
  • java -cp ".:target/helloworld-1.0-SNAPSHOT.jar:lib/*" com.hello.App this is the commandline I am using . – 0decimal0 Mar 24 '18 at 19:50
  • No, please [edit] your question and include the information there. – Jim Garrison Mar 24 '18 at 19:52
  • 1
    OK, now run `mvn dependency:tree` to see the COMPLETE list of dependendies and make sure you have all the necessary libraries in the `lib` directory. – Jim Garrison Mar 24 '18 at 19:57
  • Since you're using Maven, you shouldn't put jar files in your lib folder for the same dependencies – OneCricketeer Mar 24 '18 at 20:03
  • @JimGarrison Thanks a lot Jim you made my life easier . After looking at dependency tree I imported all the necessary jar files to lib folder it were quite a few . Your comment should be an answer because it is of a great help. – 0decimal0 Mar 24 '18 at 20:08
  • @0decimal0 done – Jim Garrison Mar 24 '18 at 20:55

2 Answers2

1

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

miskender
  • 7,460
  • 1
  • 19
  • 23
1

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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • Using mvn exec:java gives the error --- org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid . I have set plugin dependencies for maven in pom.xml. – 0decimal0 Mar 25 '18 at 12:29
  • never mind :) finally got it to run through mvn exec . – 0decimal0 Mar 25 '18 at 12:40