Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 709
matteoh Avatar asked Sep 01 '16 14:09

matteoh


People also ask

How do I get Java 16 in IntelliJ?

IntelliJ IDEA Configuration You can also download Java 16 directly from IntelliJ IDEA. To do so, go to Platform Settings and click on SDKs, then click the '+' sign at the top, choose Download JDK, then select the Vendor and version and the directory to download the JDK to.

Is IntelliJ Java free?

IntelliJ IDEA is available in the following editions: Community Edition is free and open-source, licensed under Apache 2.0. It provides all the basic features for JVM and Android development. IntelliJ IDEA Ultimate is commercial, distributed with a 30-day trial period.

How do I install an older version of IntelliJ?

Click on the "Download" button of the product you need, and you will be taken to the 30-day free trial download page for the current version. On the product download page, there is also a "previous versions" link which you can use to choose and download the previous versions of the product.


1 Answers

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.

like image 165
matteoh Avatar answered Sep 22 '22 12:09

matteoh