开发者

executing jar file: Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext

I have created a maven project. After I type: mvn clean package everything works fine and I see my jar file inside target. But if I click the file I get the follwing error message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
ontext/ApplicationContext
Caused by: java.lang.ClassNotFoundException: org.springframework.context.Applica
tionContext
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: comt.test.Test. Program will exit

I can run the project under Eclipse and works ok, but the jar file doesnt work.

This is the content of Manifest:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Build-Jdk: 1.6.0_23
Main-Class: com.stlpo.App
Class-Path: commons-httpclient-3.1.jar commons-codec-1.2.jar h2-1.2.138.jar 
commons-lang-2.5.jar spring-core-3.0.3.RELEASE.jar spring-asm-3.0.3.RELEASE.jar co
 mmons-logging-1.1.1.jar spring-context-3.0.3.RELEASE.jar spring-beans
 -3.0.3.RELEASE.jar spring-expression-3.0.3.RELEASE.jar spring-orm-3.0
 .3.RELEASE.jar spring-tx-3.0.3.RELEASE.jar spring-aop-3.0.3.RELEASE.j
 ar aopalliance-1.0.jar spring-jdbc-3.0.3.RELEASE.jar spring-test-3.0.
 3.RELEASE.jar cglib-nodep-2.2.jar logback-core-0.9.24.jar logback-cla
 ssic-0.9.24.jar slf4j-api-1.6.0.jar hibernate-annotations-3.5.4-Final
 .jar hiber开发者_StackOverflow中文版nate-core-3.5.4-Final.jar antlr-2.7.6.jar commons-collectio
 ns-3.1.jar jta-1.1.jar hibernate-commons-annotations-3.2.0.Final.jar 
 hibernate-jpa-2.0-api-1.0.0.Final.jar hibernate-entitymanager-3.5.4-F
 inal.jar cglib-2.2.jar asm-3.1.jar javassist-3.9.0.GA.jar opencsv-2.1
 .jar jfreechart-1.0.13.jar jcommon-1.0.16.jar commons-beanutils-1.8.3
 .jar beansbinding-1.2.1.jar poi-3.6.jar log4j-1.2.13.jar
  poi-ooxml-3.6.jar poi-ooxml-schemas-3.6.jar xmlbeans-2.3.0.jar stax-
 api-1.0.1.jar geronimo-stax-api_1.0_spec-1.0.jar dom4j-1.6.1.jar xml-
 apis-1.0.b2.jar commons-dbcp-1.4.jar commons-pool-1.5.4.jar TableLayo
 ut-20050920.jar

Thanks in advance for your help.


When you build a JAR, it only has your classes in it. It doesn't contain all the other libraries that your project depends on. (Doing that is possible, but it's not common and mostly inadvisable.) To run a class in your JAR that depends on Spring, you have to be sure Spring is on the classpath, either by passing a classpath to the java executable (doesn't work if using the -jar argument) or by putting a "Class-Path" attribute in the JAR's manifest file. Of course, it's not just Spring. You'll have to include a classpath entry for every jar your project depends on.


see another post regarding the same problem you need to create a classPath (the eclipse is dong it automatically for you, and this is why you have no problem ruining the project from the eclipse) for the dependencies (in your case the spring)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>fully.qualified.MainClass</mainClass> 
                <addClasspath>true</addClasspath>
                <!-- use class path prefix if your dependencies are out side the jar)
                <classpathPrefix>lib/</classpathPrefix> -->          
            </manifest>

            <manifestEntries>
                <Class-Path>conf/</Class-Path>
            </manifestEntries>
        </archive>

    </configuration>
</plugin>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜