Issue
I started writing a basic Hello World Multi-Module project with Maven inside IntelliJ. It runs perfectly with the built-in run button, but when I build the project via command line with mvn install
and then try running it with java -cp HelloWorld/target/HelloWorld-1.0-SNAPSHOT.jar App
, it says
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)
Maybe the problem is the project structure (which was given to my by IntelliJ):
parent/
– pom.xml
– HelloWorld/
– – pom.xml
– – src/main/java/App.java
– GetMoreData/
– – pom.xml
– – src/main/java/MoreDataClass.java
The App.java
inside the HelloWorld
-Module looks like this:
public class App {
public static void main(String[] args) {
System.out.println("Hello World");
MoreDataClass c = new MoreDataClass();
System.out.println(c.getMoreData());
}
}
And the MoreDataClass
inside GetMoreData
-Module contains this code:
public class MoreDataClass {
public String getMoreData() {
return "Here is some more data from the Module GetMoreData";
}
}
Running App.main()
in IntelliJ returns (as supposed to) the following lines at standard out:
Hello World
Here is some more data from the Module GetMoreData
Running it via Maven in the command line (as described at the beginning) on the other hand returns this:
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
So the App.main()
is built and can be executed, but it doesn't find the MoreDataClass
. Why doesn't it?
In case those are relevant to the problem, here are my 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>
Solution
You need to add GetMoreData
jar to classpath, or configure the HelloWorld
s build to generate jar-with-dependencies
Answered By - user10639668
Answer Checked By - Pedro (JavaFixing Volunteer)