Maven: Running created Jar-File with depedencies [duplicate] - java

This question already has answers here:
How can I create an executable/runnable JAR with dependencies using Maven?
(33 answers)
Closed 8 years ago.
I've written a little Java command line tool, that I want to run by (for example)
java -jar myJarFile.jar de.my.path.MainClass arg0 arg1 arg2
For building the jar-File and managing the depedencies I am using Maven. Now I found out, that Maven does not include the depedencies of the POM-File in the Jar-File. So, when I run my jar-file, I get ClassNotFoundExceptions.. But to execute my jar-file I need those libraries in the classpath.
How can I manage this with Maven?
Thank you for your help!!

Put this maven assembly plugin in your pom.xml:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>de.my.path.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

There are multiple options.
maven app assembly plugin
This package all dependent jars and also creates linux/ windows scripts to run your application. See Here for more information on how to use it.
Maven assembly plugin
It creates single executable jar including all dependencies. More here. However i found this problematic with some third party jars(spring) where there are files with same names.

Related

No suitable driver found for jdbc:postgresql://localhost:5432/gis

I've spent the last 3 hours trying to get my Java program to interface with my Postgres server. I cannot get past the error message "No suitable driver found for jdbc:postgresql://localhost:5432/gis". It is a Bukkit plugin, and I am using IntelliJ IDEA.
The code:
try
{
//Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/gis");
}
catch(Exception e)
{
getLogger().info(e.getMessage());
}
Things I have tried:
java -cp ./path/to/postgresjdbc.jar -jar spigot-1.15.2.jar
adding the jdbc file internals directly to my jar file
adding the jdbc file as a dependency within the IntelliJ project
switching to maven, and putting the following in pom.xml:
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.1</version>
</dependency>
</dependencies>
I am unable to get past the error I posted. At this point, it has taken over my entire evening.
I've stumbled with this issue several times when developing Bukkit/Spigot plugins that make use of MySQL databases. The process for Postgres should be the same.
Usually, this error happens when your plugin can't find the PostgresqlJDBC driver. You have two workarounds:
Option 1. Adding the .jar to the plugin's classpath:
It's recommended that you set the libraries inside plugins/lib as then your server won't try to load the libraries as a Bukkit plugin. Add the prefix lib/ to your classpath by adding this configuration in your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath> <-- don't know if this is needed -->
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
Then make sure to put your postgresjdbc.jar inside a folder called lib inside your plugin's folder.
Option 2. Add dependencies directly in your jar:
Note that this option will increase your plugin's jar size.
This can be done via Maven's assembly plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your.main.class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
If you open your plugin's jar with a file compressor like 7-zip you should there should the driver's classes in it apart from your plugin ones.
Let me know if this solved your issue.

How to setup Maven to generate regular as well as executable JAR files from a single POM [duplicate]

This question already has answers here:
How can I create an executable/runnable JAR with dependencies using Maven?
(33 answers)
Closed 5 years ago.
I know how to setup Maven to generate a regular JAR file for my project with mvn package. I also know how to setup Maven to generate an executable JAR file with the same command. What I would like to be able to do is to generate a regular JAR file with mvn package and also generate an executable JAR file, either from the same command or with another mvn command. All setup in the same POM file. Thanks!
add below plugin into your pom.xml:
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.demo.MainTestClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
after adding this you need to run mvn clean compile assembly:single command from command line.

How i start java Class with package from Maven (Intellij)

I have a problem.
I write a Java console app in intelliJ. I add the maven package org.json.json and if i run the project in IntelliJ everything works.
If I start the class in the console with
java Main I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/json/JSONObject
I have try and search a lot but nothing works
The maven-shade-plugin is a fancy plugin with plenty of options. For creating simpler jars containing jars I use the maven-assembly-plugin. Also verify that the scope within the org.json.json dependency is set to compile.
The details are documented at: https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#jar-with-dependencies
For example:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your.main.class.package.your_main_class</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
Just running a class from a console is not enough. You also need to set classpath to point to all of its dependencies including json jars.
Since you are using Maven, my advice is to use its "maven-shade-plugin" in order to create one large jar that has your class and all of json jar classes and run your class with "-jar" option.

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

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!
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.
Okay, so you basically want to create a "fat jar" (sometimes called assembly), that contains all its own dependencies (usually, the dependencies are external).
You need to use a Maven plugin for that. Below is a sample assembly plugin configuration jar-with-dependencies:
<project>
...
<build>
...
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
...
</project>
then, simply run
mvn package

Jar'ing Java project, and not one class

I want to create a jar file of a Java project, which compiles. I've looked on the internet but all the examples of how to do this seem to take one java file and work on that. I want to jar the root of the java project. How is this possible?
Thanks
IntelliJ
According to this Stackoverflow topic you can built jar file using
You didn't specify your IDEA version.
Before 9.0 use Build | Build Jars, in
IDEA 9.0 use Project Structure |
Artifacts.
Maven
You should learn to use a build tool like maven. You then could use the maven assembly plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>*******your main class******</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
Ant
Or you could use ant(2) to create a jar file.
Here is a link to the tutorial that might help.
The jar command takes either files or directories. So you can simply specify the name of the directory that you want to jar rather than a file name. Once you know the command to jar a single file then you can jar a directory. More details here.
And this tutorial shows an example of jaring a file and a number of directories together.

Categories