Maven Multi-Module-Project可在IntelliJ中运行,但通过命令行运行时显示“ java.lang.NoClassDefFoundError”

时间:2018-12-09 20:35:00

标签: java maven intellij-idea

我开始在IntelliJ中用Maven编写一个基本的Hello World多模块项目。它可以通过内置的运行按钮完美运行,但是当我使用mvn install通过命令行构建项目,然后尝试使用java -cp HelloWorld/target/HelloWorld-1.0-SNAPSHOT.jar App运行该项目时,它会显示

  

线程“主”中的异常java.lang.NoClassDefFoundError:   App.main(App.java:5)的MoreDataClass由以下原因引起:   java.lang.ClassNotFoundException:MoreDataClass位于   java.net.URLClassLoader.findClass(URLClassLoader.java:381)在   java.lang.ClassLoader.loadClass(ClassLoader.java:424)在   sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:335)在   java.lang.ClassLoader.loadClass(ClassLoader.java:357)

也许问题出在项目结构(这是IntelliJ给我的):

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

App.java模块内部的HelloWorld看起来像这样:

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

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

MoreDataClass模块内部的GetMoreData包含以下代码:

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>

1 个答案:

答案 0 :(得分:1)

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