0

我使用 Maven install 创建了一个 jar,当我使用命令运行 jar 时,java -jar MyJar.jar 它给了我异常。下面是堆栈跟踪。

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
        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)

我的 pom.xml 中有所有依赖项,并且我成功地将主类作为独立应用程序运行。

然后我提取了 jar 的内容,虽然 Manifest 文件在类路径中包含所有 jar,但我很惊讶地发现其中没有依赖 jar。下面是生成的清单文件:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Anand Jain
Build-Jdk: 1.6.0_38
Main-Class: com.hcentive.simulator.cms.beq.response.util.Test
Class-Path: spring-jdbc-3.0.6.RELEASE.jar spring-beans-3.0.6.RELEASE.j
 ar spring-core-3.0.6.RELEASE.jar spring-tx-3.0.6.RELEASE.jar spring-c
 ontext-3.0.6.RELEASE.jar spring-expression-3.0.6.RELEASE.jar spring-a
 sm-3.0.6.RELEASE.jar cglib-nodep-2.2.jar hibernate-jpa-2.0-api-1.0.1.
 Final.jar spring-aop-3.0.6.RELEASE.jar aopalliance-1.0.jar spring-bat
 ch-core-2.1.7.RELEASE.jar xstream-1.3.jar xpp3_min-1.1.4c.jar jettiso
 n-1.1.jar spring-batch-infrastructure-2.1.7.RELEASE.jar commons-beanu
 tils-1.8.3.jar commons-logging-1.1.1.jar hibernate-core-3.5.1-Final.j
 ar antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar xml-ap
 is-1.0.b2.jar jta-1.1.jar slf4j-api-1.5.8.jar commons-dbcp-1.2.2.jar 
 commons-pool-1.3.jar commons-io-1.3.2.jar commons-lang-2.5.jar hsqldb
 -1.8.0.7.jar aspectjrt-1.6.8.jar aspectjweaver-1.6.8.jar log4j-1.2.14
 .jar

有人可以帮忙吗?

4

2 回答 2

2

要使用 maven 构建包含所有依赖项的 jar,您可以添加...

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.example.MyMain</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-jar-with-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

您还应该确保为每个依赖项正确设置范围。

于 2013-01-30T06:07:56.003 回答
-2

java -classpath jar1;jar2;jar3;jar4;xml1;xml2;....; jar ExceutableJar.jar

I believe it might be having a lot of jar file

I recommend you you to copy the entire command in to a notepad file and save it as batch (.bat) or shell script (.sh) in case of Unix

So that , you can run the jar easily with one file, instead of repeating the command

于 2013-01-30T05:47:34.963 回答