[英]Project runs in Eclipse, but not from command line : java.lang.NoClassDefFoundError with Maven and Dom4J
I made a small sample project to test things out and came across something that I found weird. 我做了一个小样本项目来测试事物,并发现了一些我觉得很奇怪的东西。 I made a new Maven Project in Eclipse with some code that pokes around in XML files using Dom4J.
我在Eclipse中创建了一个新的Maven项目,其中包含一些使用Dom4J在XML文件中查找的代码。 It runs fine as long as I only start the App from withing Eclipse, however when I want to start the jar that maven packaged for me I get this Error:
只要我仅从Eclipse中启动应用程序,它就可以正常运行,但是当我想启动为我打包了maven的jar时,出现此错误:
avalon% java -jar target/avalon.launcher-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.privateGetMethodRecursive(Class.java:3035)
at java.lang.Class.getMethod0(Class.java:3005)
at java.lang.Class.getMethod(Class.java:1771)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
There must be something wrong with my setup. 我的设置一定有问题。 Here is my pom.xml:
这是我的pom.xml:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.oliverconzen</groupId>
<artifactId>avalon.launcher</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>avalon</name>
<description>Simple cli based game written in Java</description>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>de.oliverconzen.avalon.launcher.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Thank you very much! 非常感谢你!
Of course, second after I post this, I found an answer. 当然,在我发布此消息后第二次,我找到了答案。 The dependencies don't get bundled for execution.
依赖关系不会捆绑在一起执行。 If they are not bundled, the jars of dependencies have to be in the same folder (?).
如果未捆绑在一起,则依赖项的jar必须位于同一文件夹(?)中。
Anyway, this helped me: 无论如何,这对我有帮助:
How can I create an executable JAR with dependencies using Maven? 如何使用Maven创建具有依赖项的可执行JAR?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.