gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:29:25 25 4
gpt4 key购买 nike

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

Exception in thread "main" java.lang.NoClassDefFoundError:MoreDataClass at App.main(App.java:5) Caused by:java.lang.ClassNotFoundException: MoreDataClass atjava.net.URLClassLoader.findClass(URLClassLoader.java:381) atjava.lang.ClassLoader.loadClass(ClassLoader.java:424) atsun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) atjava.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";
}
}

正在运行App.main() IntelliJ 在标准输出中返回(按预期)以下几行:

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>

最佳答案

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

关于java - Maven 多模块项目在 IntelliJ 中工作,但通过命令行运行显示 "java.lang.NoClassDefFoundError",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53696423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com