0

I am using IntelliJ version 11.0.7 (2020.1.3) created a simple maven project and added my jar to it by

File -> Project Structure -> New Project Library -> Java -> Selected my jar -> Ok -> Ok

in that jar file, all the dependencies present which requires to run the application.

There are no compile-time errors but when I run my maven project then it is throwing this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException

After adding this jar it is throwing an exception about the next missing jar, likewise when I added all the dependencies which are used inside that jar then everything works fine.

Is there any way to auto-generate all the dependencies and add to External Libraries from the jar when I added to it?

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • see https://stackoverflow.com/a/574650/6629 basically you either need to create an 'all in one' jar, with all the dependencies added to the executable jar, or you need to give classpath to all the jars. – Kinjal Dixit Sep 30 '20 at 05:06
  • You also need to define your dependencies (libraries) in maven, not in IntelliJ. – dan1st Sep 30 '20 at 05:10

2 Answers2

0

in that jar file, there are all the dependencies present which requires to run the application.

Are you sure that all dependencies present? Your next statement saying After adding this jar it is throwing exception about the next missing jar, likewise when I added all the dependencies which are used inside that jar then everything works fine.

Is your jar file hosted in maven repository? If yes, simply declare it in maven pom.xml file, it will manage all the transitive dependencies. If it is not in maven repository, you need to run mvn install command to install that into your local maven repository, later on refer it in your pom.xml file. It will auto resolve your transitive dependencies as well, if you package your jar file properly which include correct pom.xml inside.

Sam YC
  • 10,725
  • 19
  • 102
  • 158
  • Yes, all the dependencies have existed in the `pom.xml` inside the jar file. No, it is not hosted in maven repo. I run `mvn install` command as well but it is not installing the transitive dependencies. – Shailendra Madda Sep 30 '20 at 05:32
  • That mean your install command doesn't run properly, you can refer to this answer for more clue https://stackoverflow.com/a/36559812/1542363 – Sam YC Sep 30 '20 at 05:45
0

Finally, I added this in my pom.xml where I generated the Jar file

To get Maven to build a Fat JAR from your project you must include a Fat JAR build configuration in your project's POM file. You configure Maven to build a Fat JAR from your project by including the maven-assembly-plugin in your POM file's plugin section. Maven refers to an output product that it builds as an assembly. Hence the name maven-assembly-plugin.

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

Now it is working as expected.

Reference: http://tutorials.jenkov.com/maven/maven-build-fat-jar.html

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138