[英]Issue while running executable jar : ClassNotFoundException coming
I created a jar using Maven install and when i ran the jar using command java -jar MyJar.jar
it gave me the exception. 我使用Maven安装创建了一个jar,当我使用命令
java -jar MyJar.jar
运行jar时,出现了异常。 Below is the stack trace. 下面是堆栈跟踪。
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)
I had all dependencies in my pom.xml and i successfully ran the main class as standalone application. 我在pom.xml中拥有所有依赖关系,并且成功地将主类作为独立应用程序运行。
Then I extracted the contents of jar and I was surprised to see there were no dependency jars in it though Manifest file has all the jars in classpath. 然后我提取了jar的内容,尽管清单文件中所有jar都在classpath中,但我惊讶地发现其中没有依赖jar。 Below is the Manifest file generated:
下面是生成的清单文件:
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
Can someone please help? 有人可以帮忙吗?
To build a jar containing all dependencies using maven, you can add... 要使用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>
You should also ensure that the scope is set correctly for each dependency. 您还应确保为每个依赖项正确设置了范围。
java -classpath jar1;jar2;jar3;jar4;xml1;xml2;....; java -classpath jar1; jar2; jar3; jar4; xml1; xml2; ....; jar ExceutableJar.jar
罐ExceutableJar.jar
I believe it might be having a lot of jar file 我相信可能有很多jar文件
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 我建议您将整个命令复制到记事本文件中,如果是Unix,则将其另存为批处理(.bat)或shell脚本(.sh)。
So that , you can run the jar easily with one file, instead of repeating the command 这样一来,您只需一个文件即可轻松运行jar,而无需重复执行该命令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.