mvn install 之后,我尝试运行 main,但在 ConfigurationException 中收到 NoClassDefFoundError
我创建了一个 Maven 项目,我已经编写了几个星期的应用程序,并希望最终运行它......但我不能。
执行 .jar 时,我得到以下堆栈跟踪:
C:\Users\MyUser\.m2\repository\com\myCompany\MyApp\
0.0.1-SNAPSHOT> java -Dconfigfile=config.xml -jar MyApp-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/configuration/ConfigurationException
at com.mycompany.connector.DBConnector.reloadDBSettings(DBConnector.java:23)
at com.mycompany.connector.DBConnector.<clinit>(DBConnector.java:16)
at com.mycompany.connector.DBConnectorFactory.createDBConnector(DBConnectorFactory.java:17)
at com.mycompany.service.MyAppService.<clinit>(MyAppService.java:38)
at com.mycompany.main.MyAppMain.init(MyAppMain.java:13)
at com.mycompany.main.MyAppMain.main(MyAppMain.java:8)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.configuration.ConfigurationException
at java.net.URLClassLoader$1.run(Unknown Source)
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)
... 6 more
org.apache.commons.configuration.ConfigurationException 已导入到类中,并且我设置 Maven 将其用作依赖项。我做错了什么?
顺便说一句:这是我的 reloadDBSettings 方法:
import java.io.FileNotFoundException;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
public final class ConfigurationHandler {
private static XMLConfiguration config;
/**
* to reload Configuration-File if necessary
*/
public static void reloadConfig() throws FileNotFoundException {
try {
config = new XMLConfiguration(System.getProperty("configfile"));
} catch (ConfigurationException e) {
throw new FileNotFoundException("Es wurde keine Konfiguration geladen - Überprüfen Sie die Systemvariable 'configfile'(" + e.getMessage() + ")");
}
}
}
I created a maven project and I have written for weeks an application and want to run it in the end... But I can't.
I get the following stacktrace when executing my .jar:
C:\Users\MyUser\.m2\repository\com\myCompany\MyApp\
0.0.1-SNAPSHOT> java -Dconfigfile=config.xml -jar MyApp-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/configuration/ConfigurationException
at com.mycompany.connector.DBConnector.reloadDBSettings(DBConnector.java:23)
at com.mycompany.connector.DBConnector.<clinit>(DBConnector.java:16)
at com.mycompany.connector.DBConnectorFactory.createDBConnector(DBConnectorFactory.java:17)
at com.mycompany.service.MyAppService.<clinit>(MyAppService.java:38)
at com.mycompany.main.MyAppMain.init(MyAppMain.java:13)
at com.mycompany.main.MyAppMain.main(MyAppMain.java:8)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.configuration.ConfigurationException
at java.net.URLClassLoader$1.run(Unknown Source)
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)
... 6 more
org.apache.commons.configuration.ConfigurationException is imported to the class and I set up Maven to use it as dependency. What am I doing wrong?
BTW: this is my reloadDBSettings Method:
import java.io.FileNotFoundException;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
public final class ConfigurationHandler {
private static XMLConfiguration config;
/**
* to reload Configuration-File if necessary
*/
public static void reloadConfig() throws FileNotFoundException {
try {
config = new XMLConfiguration(System.getProperty("configfile"));
} catch (ConfigurationException e) {
throw new FileNotFoundException("Es wurde keine Konfiguration geladen - Überprüfen Sie die Systemvariable 'configfile'(" + e.getMessage() + ")");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
java.lang.ClassNotFoundException
意味着您的$CLASSPATH
变量中没有org.apache.commons.configuration.ConfigurationException
你的 shell 中,在命令行上运行java
命令与通过 Maven 运行/编译不同。A
java.lang.ClassNotFoundException
means that you don't have theorg.apache.commons.configuration.ConfigurationException
in your$CLASSPATH
variable in your shell, and running ajava
command on the command-line is not the same as running/compiling through Maven.使用 exec:java。 Maven 将在类路径中包含所有必需的类。
添加配置(在 pom.xml 中的
下):通过
mvn exec:java
运行Use exec:java. Maven will include all necessary classes on the classpath.
Add configuration (under
<plugins>
in your pom.xml):Run via
mvn exec:java
ClassNotFoundException
表示 JVM 无法在类路径中找到该类。这与 NoClassDefFoundError 不同,NoClassDefFoundError 意味着找不到该类使用的类(不是类本身)。有两种方法可以解决这个问题:
将所有依赖的 jar 文件添加到一个文件夹中,将其添加到 JVM 类路径中并使用以下命令运行主类:
java -cp .\dependency\*;MyApp-0.0.1-SNAPSHOT.jar.jar com.myCompany.MyApp.Main
为了将所有依赖项放入一个文件夹中,您可以使用 Maven 依赖项插件
maven-dependency-plugin
使用maven程序集插件
maven- assembly-plugin
。该插件会将所有依赖的 jar 文件捆绑到一个单独的 jar 文件中,以便您可以像以前一样执行它。但是,有时此方法不起作用,特别是对于使用大量库的大型应用程序。这些 jar 可能会相互冲突,因此您可能需要自己解决依赖关系或使用 Tattletale maven 插件来解决它为您服务。注意:Tattletale 也可以很好地解决第一种情况下的冲突。
ClassNotFoundException
means JVM cannot find the class in your classpath. This is different fromNoClassDefFoundError
which means the classes use by that class are not found (not the class itself).There are two ways to solve this problem:
add all the dependent jar files to a folder, add that to JVM classpath and run your main class using:
java -cp .\dependency\*;MyApp-0.0.1-SNAPSHOT.jar.jar com.myCompany.MyApp.Main
and in order to get all the dependencies into a folder you can use maven dependencies plugin
maven-dependency-plugin
use maven assembly plugin
maven-assembly-plugin
. This plugin will bundle all the dependent jar files in to a signle jar file so you can execute it the same way you do. However, sometime this method does not work especially for a larger application that use a lot of libraries. The jars may conflict each other so you may need to resolve the dependencies yourself or use Tattletale maven plugin to solve it for you.Note: Tattletale is also good to solve conflict in the first case.