Intellij Idea Maven多模块-项目在IntelliJ中工作,但通过命令行运行时显示“java.lang.NoClassDefFoundError”

qncylg1j  于 2022-11-21  发布在  Maven
关注(0)|答案(1)|浏览(184)

我开始在IntelliJ中使用Maven编写一个基本的Hello World多模块项目。
线程“main”中出现异常java.lang.NoClassDefFoundError:应用程序main(App.java:5)中的MoreDataClass导致的原因:java.lang.ClassNotFoundException:在java.net中找到了一个类加载器。在java.lang中找到了一个类加载器。在sun中找到了一个类加载器。
也许问题出在项目结构上(这是IntelliJ给我的):

parent/
– pom.xml
– HelloWorld/
– – pom.xml
– – src/main/java/App.java
– GetMoreData/
– – pom.xml
– – src/main/java/MoreDataClass.java

HelloWorld-模块中的App.java如下所示:

public class App {
    public static void main(String[] args) {
        System.out.println("Hello World");

        MoreDataClass c = new MoreDataClass();
        System.out.println(c.getMoreData());
    }
}

GetMoreData-模块中的MoreDataClass包含以下代码:

public class MoreDataClass {
    public String getMoreData() {
        return "Here is some more data from the Module GetMoreData";
    }
}

在IntelliJ中运行App.main()会在标准输出处返回(如预期)以下行:

Hello World
Here is some more data from the Module GetMoreData

另一方面,在命令行中通过Maven运行它(如开头所述)将返回以下内容:

Hello World
Exception in thread "main" java.lang.NoClassDefFoundError: MoreDataClass
    at App.main(App.java:5)
Caused by: java.lang.ClassNotFoundException: MoreDataClass
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

所以App.main()已经被编译并且可以执行了,但是它没有找到MoreDataClass,为什么没有呢?
如果这些都与问题相关,下面是我的pom.xml:

****** parent/pom.xml: ******

<?xml version="1.0" encoding="UTF-8"?>
<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.playground.multiModule</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>GetMoreData</module>
        <module>HelloWord</module>
    </modules>

</project>

***** parent/GetMoreData/pom.xml *****

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>de.playground.multiModule</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>GetMoreData</artifactId>

</project>

***** parent/HelloWorld/pom.xml *****

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>de.playground.multiModule</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>HelloWord</artifactId>

    <dependencies>
        <dependency>
            <groupId>de.playground.multiModule</groupId>
            <artifactId>GetMoreData</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>
z8dt9xmd

z8dt9xmd1#

您需要将GetMoreData jar添加到类路径中,或者配置HelloWorld的构建以生成jar-with-dependencies

相关问题