0

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?

Dmitry Rakovets
  • 557
  • 1
  • 6
  • 15
jumbodrawn
  • 128
  • 1
  • 9
  • 2
    In Eclipse you have an option to package all required jar files into a single output jar file. Right click on project -> Export... -> Runnable jar file – Abishek Stephen Sep 13 '21 at 20:51
  • 1
    The other option is to copy the external dependencies to the folder with your jar or a valid classpath. – sorifiend Sep 13 '21 at 20:53
  • 1
    How do you run the JAR? Please add the complete command line you use to your question. And, off-topic, please consider changing the background (colors) of your web site. 1. black on magenta is hard to read and 2. I'm suffering from beginning eye cancer now. ;) – Gerold Broser Sep 13 '21 at 21:02

1 Answers1

2

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.

jumbodrawn
  • 128
  • 1
  • 9
  • 2
    The `pluginManagement` tag defines the configurations of plugins *if they are used* but doesn't actually configure them to be used. It provides one place for default configuration in a multi-module project. – tgdavies Sep 13 '21 at 21:16
  • @tgdavies Not only "in a multi-module project". Think of declaring the usage of the same plugin more than once in one and the same POM. Otherwise you're correct. – Gerold Broser Sep 17 '21 at 11:56