简体   繁体   中英

Simple Spring and Maven Application - java.lang.NoClassDefFoundError Exception

Because I have some weird issues with my Maven application, I have decided to create the most simple Spring standalone Java application managed with Maven to find out where my problem is. I'm attaching my all code, the code is the simplest as can be but something is wrong. I must be blind but do you see what could causes this Exception?

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

My system setting:

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

what is not set is CLASS_PATH , but this is content of .classpath file in the root directory of my app - I'm not sure if the missing CLASS_PATH is not the problem. If so, where should CLASS_PATH points to on OS X and 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>

I created Maven project:

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

I set dependencies into pom.xml configuration file

<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>

Then I created simple Spring app...

Content of applicationContext.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-2.5.xsd">

    <bean id="helloBean" class="com.example.HelloWorld">
        <property name="name" value="Jirka" />
    </bean>

</beans>

and HelloWorld.java :

public class HelloWorld {
    private String name;

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

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

and 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();
    }
}

Then I packaged app into jar file:

mvn package

and tryied to run it:

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

but this errors occured:

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

Your Maven setup lets you compile your code and build the jar, but Spring isn't in the runtime classpath. Add Spring to -cp at the command line.

Or use the Maven assembly plugin to build an "uber" jar containing your code and all the dependencies, and run that.

I had the same issue with a small java project , i have used Spring and using eclipse Version: Mars.1 Release (4.5.1). All I did is right click on the project->export->runnable jar file . select the radio button copy required libraries into a sub-folder next to the generated jar and click on finish. Then go to the command prompt select the location where the Jar was created and give the following command java -jar jarname This should work .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM