Reputation: 128
I have a Maven project which uses a few external dependencies, mainly Jackson.
When I run mvn clean -e install
, it creates a jar file as specified in the target
directory. However, when I run this jar file with java in command line I get the error message:
Error: Unable to initialize main class com.test.package.Main
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
The specific element of Jackson which it's getting confused over does not matter, the error message will point out whatever portion of Jackson the code uses first. The strange part is when I run the project from within IntelliJ, it works fine. It seems that the JAR file does not have access to the proper dependencies.
This is the relevant portion of the pom.xml
:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.test.package.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<inherited>true</inherited>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Is there something I should be doing for the built JAR file to run properly anywhere?
Upvotes: 0
Views: 2889
Reputation: 128
Solved: This solution from another thread fixed my problem: https://stackoverflow.com/a/42231035/8402030 I had to add a plugin called Maven Shade which creates an uber jar containing all dependencies. Additionally had to remove the plugin from the tag. Not entirely sure why it matters that the plugin is outside of . If anyone knows, feedback would be appreciated.
Upvotes: 2