Reputation: 4585
I'm trying to create a maven based project which has a parent pom and then two modules. One for business and one for a command line application which depends on business.
The 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>dk.fitfit.triangle.parent</groupId>
<artifactId>triangle-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Business</module>
<module>Cmd</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>dk.fitfit.triangle.cmd.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Then I have the business pom
<?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>triangle-parent</artifactId>
<groupId>dk.fitfit.triangle.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>dk.fitfit.triangle.business</groupId>
<artifactId>triangle-business</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
And finally the pom file for the command line application
<?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>triangle-parent</artifactId>
<groupId>dk.fitfit.triangle.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>dk.fitfit.triangle.cmd</groupId>
<artifactId>triangle-cmd</artifactId>
<dependencies>
<dependency>
<groupId>dk.fitfit.triangle.business</groupId>
<artifactId>triangle-business</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
The problem is when I try to run my jar file I get the following error message
Exception in thread "main" java.lang.NoClassDefFoundError: dk/fitfit/triangle/business/Triangle
at dk.fitfit.triangle.cmd.Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: dk.fitfit.triangle.business.Triangle
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
I'm trying to run my jar using the following command
mvn install && mvn package && java -jar Cmd/target/triangle-cmd-1.0-SNAPSHOT.jar 1 2 3
Any clues about what I'm doing wrong?
Upvotes: 1
Views: 424
Reputation: 620
In the pom of your command line application, replace maven-jar-plugin with the following.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>dk.fitfit.triangle.cmd.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Now you'll get a fat jar which will contain all the dependencies which you can use.
Upvotes: 8
Reputation: 11132
Each module produces a separate jar, that is, you have one business-jar and one cmd-jar. When you run the java command, you only specify the cmd-jar.
Now you have two options:
Upvotes: 3