我开始在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>
1条答案
按热度按时间z8dt9xmd1#
您需要将
GetMoreData
jar添加到类路径中,或者配置HelloWorld
的构建以生成jar-with-dependencies。