Intellij Java 2016 & Maven : how to embed dependencies in JAR?

matteoh

I'm using Intellij Java 2016.2.2 and Maven to create a very simple Java console application.

I want to add an external library, so I add my dependency in Maven like this:

<dependency>
    <groupId>jline</groupId>
    <artifactId>jline</artifactId>
    <version>2.12</version>
</dependency>

It works fine when I run it in the IDE, but not in an external console (I have the following error: java.lang.NoClassDefFoundError).

I checked and for some reason, the external JAR is not added in the JAR I just generated. I also tried many things in "File -> Project Structure", but still not working...

I just want to build my JAR with my dependencies in it, so I can simply run my application in a console using:

java -jar myproject.jar

How can I do that? Thanks for your help!

matteoh

I finally managed to generate this JAR with Intellij Java, here is how I do:

  • add the dependencies in the pom.xml file
  • go to File -> Project Structure -> Artifacts -> New -> JAR -> From module with dependencies
  • choose the Main class and click OK
  • in your project, in src/main, create the "resources" folder
  • move the "META-INF" (with MANIFEST.MF in it) folder in this "resources" folder
  • go to Build -> build artifacts to build the JAR

EDIT

A better (and easier way) to do it is adding the following lines in the pom.xml file :

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>your.MainClass</mainClass>
                    </manifest>
                </archive>
                <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>
    </plugins>
</build>

then use the "clean" and "package" maven commands.

The last 3 steps above (about MANIFEST.MF) still seem to be mandatory.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Intellij Java 2016 & Maven : how to embed dependencies in JAR?

From Dev

Intellij Maven: Create jar with all library dependencies

From Dev

How to include maven dependencies in a jar file?

From Dev

Maven - How to exclude dependencies from generated JAR

From Dev

Combine java code and dependencies into one jar (executable jar) using maven

From Dev

IntelliJ adding jar dependencies

From Dev

Maven: How to have jar-with-dependencies exclude "provided" dependencies?

From Dev

How to embed a python script in a JAR/JAVA package?

From Dev

How to package a jar and all dependencies within a new jar with maven

From Dev

Building jar with dependencies maven

From Dev

Adding Maven Dependencies in Intellij

From Dev

IntelliJ not reflecting Maven dependencies

From Dev

How do I export a JavaFX Application and dependencies in the same jar in Intellij?

From Dev

How to specify dependencies needed for Java class in IntelliJ?

From Java

How can I create an executable JAR with dependencies using Maven?

From Dev

How to create maven uber jar which includes dependencies with scope provided

From Dev

How to copy jar-with-dependencies in my webapp\myfolder in maven Idea

From Dev

How to create a jar with Maven that includes dependencies in a separate folder for each dependency

From Dev

Maven not adding all dependencies to jar

From Dev

Using Maven in Ant for jar dependencies

From Dev

executable jar with dependencies using maven

From Dev

Maven generate jar dependencies then war

From Dev

Maven jar without dependencies by default

From Java

How to include Clojure dependencies to a Java project with Maven

From Dev

IntelliJ not updating updated Maven dependencies

From Dev

Intellij - Trouble with maven dependencies synchronization

From Dev

Maven Dependencies Not Imported in IntelliJ 14.1.4

From Dev

Intellij IDEA: Artifact dependencies not synchronized with Maven dependencies

From Java

IntelliJ - Maven adding external jar file but java.lang.NoClassDefFoundError

Related Related

  1. 1

    Intellij Java 2016 & Maven : how to embed dependencies in JAR?

  2. 2

    Intellij Maven: Create jar with all library dependencies

  3. 3

    How to include maven dependencies in a jar file?

  4. 4

    Maven - How to exclude dependencies from generated JAR

  5. 5

    Combine java code and dependencies into one jar (executable jar) using maven

  6. 6

    IntelliJ adding jar dependencies

  7. 7

    Maven: How to have jar-with-dependencies exclude "provided" dependencies?

  8. 8

    How to embed a python script in a JAR/JAVA package?

  9. 9

    How to package a jar and all dependencies within a new jar with maven

  10. 10

    Building jar with dependencies maven

  11. 11

    Adding Maven Dependencies in Intellij

  12. 12

    IntelliJ not reflecting Maven dependencies

  13. 13

    How do I export a JavaFX Application and dependencies in the same jar in Intellij?

  14. 14

    How to specify dependencies needed for Java class in IntelliJ?

  15. 15

    How can I create an executable JAR with dependencies using Maven?

  16. 16

    How to create maven uber jar which includes dependencies with scope provided

  17. 17

    How to copy jar-with-dependencies in my webapp\myfolder in maven Idea

  18. 18

    How to create a jar with Maven that includes dependencies in a separate folder for each dependency

  19. 19

    Maven not adding all dependencies to jar

  20. 20

    Using Maven in Ant for jar dependencies

  21. 21

    executable jar with dependencies using maven

  22. 22

    Maven generate jar dependencies then war

  23. 23

    Maven jar without dependencies by default

  24. 24

    How to include Clojure dependencies to a Java project with Maven

  25. 25

    IntelliJ not updating updated Maven dependencies

  26. 26

    Intellij - Trouble with maven dependencies synchronization

  27. 27

    Maven Dependencies Not Imported in IntelliJ 14.1.4

  28. 28

    Intellij IDEA: Artifact dependencies not synchronized with Maven dependencies

  29. 29

    IntelliJ - Maven adding external jar file but java.lang.NoClassDefFoundError

HotTag

Archive