mvn install 之后,我尝试运行 main,但在 ConfigurationException 中收到 NoClassDefFoundError

发布于 2024-12-14 06:01:34 字数 2266 浏览 0 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑着哭最痛 2024-12-21 06:01:34

java.lang.ClassNotFoundException 意味着您的 $CLASSPATH 变量中没有 org.apache.commons.configuration.ConfigurationException你的 shell 中,在命令行上运行 java 命令与通过 Maven 运行/编译不同。

A java.lang.ClassNotFoundException means that you don't have the org.apache.commons.configuration.ConfigurationException in your $CLASSPATH variable in your shell, and running a java command on the command-line is not the same as running/compiling through Maven.

辞慾 2024-12-21 06:01:34

使用 exec:java。 Maven 将在类路径中包含所有必需的类。

添加配置(在 pom.xml 中的 下):

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
   <configuration>
      <arguments>
        <argument>-Dconfigfile=config.xml</argument>
      </arguments>
      <mainClass>com.tele2.main.MyAppMain</mainClass>
    </configuration>
</plugin>

通过 mvn exec:java 运行

Use exec:java. Maven will include all necessary classes on the classpath.

Add configuration (under <plugins> in your pom.xml):

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
   <configuration>
      <arguments>
        <argument>-Dconfigfile=config.xml</argument>
      </arguments>
      <mainClass>com.tele2.main.MyAppMain</mainClass>
    </configuration>
</plugin>

Run via mvn exec:java

你是暖光i 2024-12-21 06:01:34

ClassNotFoundException 表示 JVM 无法在类路径中找到该类。这与 NoClassDefFoundError 不同,NoClassDefFoundError 意味着找不到该类使用的类(不是类本身)。

有两种方法可以解决这个问题:

  1. 将所有依赖的 jar 文件添加到一个文件夹中,将其添加到 JVM 类路径中并使用以下命令运行主类:

    java -cp .\dependency\*;MyApp-0.0.1-SNAPSHOT.jar.jar com.myCompany.MyApp.Main

    为了将所有依赖项放入一个文件夹中,您可以使用 Maven 依赖项插件 maven-dependency-plugin

  2. 使用maven程序集插件maven- assembly-plugin。该插件会将所有依赖的 jar 文件捆绑到一个单独的 jar 文件中,以便您可以像以前一样执行它。但是,有时此方法不起作用,特别是对于使用大量库的大型应用程序。这些 jar 可能会相互冲突,因此您可能需要自己解决依赖关系或使用 Tattletale maven 插件来解决它为您服务。

注意:Tattletale 也可以很好地解决第一种情况下的冲突。

ClassNotFoundException means JVM cannot find the class in your classpath. This is different from NoClassDefFoundError which means the classes use by that class are not found (not the class itself).

There are two ways to solve this problem:

  1. 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

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文