This question already has answers here:
How can I create an executable/runnable JAR with dependencies using Maven?
(33 answers)
Closed 4 years ago.
I have a simple program that parse csv file into json objects.
I added json.simple-1.1 into my maven. The content of my maven is like this:
<groupId>IoT</groupId>
<artifactId>ETL2</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>ETL2</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
I try to execute the jar artifact in the terminal.
I get these errors.
Error: A JNI error has occurred, please check your installation and
try again Exception in thread "main" java.lang.NoClassDefFoundError:
org/json/simple/parser/ParseException at
java.lang.Class.getDeclaredMethods0(Native Method) at
java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at
java.lang.Class.privateGetMethodRecursive(Class.java:3048) at
java.lang.Class.getMethod0(Class.java:3018) at
java.lang.Class.getMethod(Class.java:1784) at
sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at
sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
> Caused by: java.lang.ClassNotFoundException:
org.json.simple.parser.ParseException at
java.net.URLClassLoader.findClass(URLClassLoader.java:381) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 7 more
I read in here that
This is caused when there is a class file that your code depends on
and it is present at compile time but not found at runtime. Look for
differences in your build time and runtime classpaths.
That's why I suspect that my maven configuration might not be correct.
Change your plugin to the below snippet and it is highly recommended you read How to build an executable jar with dependencies
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>ETL2</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
Related
This question already has answers here:
How can I create an executable/runnable JAR with dependencies using Maven?
(33 answers)
Closed 1 year ago.
Whenever I try to run my project as maven install, it creates a completely normal jar file. It is a swing gui application. However, when I click a button in the GUI that uses this websocket client, it ouputs the error below.
Exception in thread "Thread-2" java.lang.NoClassDefFoundError: com/neovisionaries/ws/client/WebSocketException
at bot.BotFunc.run(BotFunc.java:30)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: com.neovisionaries.ws.client.WebSocketException
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 2 more
my pom.xml looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>HelixBot</groupId>
<artifactId>HelixBot</artifactId>
<version>1.1</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>gui.GUIMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.neovisionaries</groupId>
<artifactId>nv-websocket-client</artifactId>
<version>2.14</version>
</dependency>
</dependencies>
<packaging>jar</packaging>
</project>
As you can see, the correct dependency is added, so I am not sure why this error is appearing. I have tried clean building it and checked the .project and .classpath files. It may be due to the fact that I converted it to maven from a regular java project, but I have not found anything. Please let me know if there is a possible solution.
If you are saying that you run maven install and gets a "normal" jar file, maybe you are missing the plugin to add the dependencies to your jar. So after you run a simple mvn clean package, and run the jar file. I believe that could work.
<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>
</configuration>
</plugin>
We have this small program we made and we wanna create an executable jar. Screenshot of my maven dependency and the project structure The problem is that I use Maven install and create a jar and in my pom, I have this plugin:
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
I have been trying Project.Main and as in the code just Main ( Main) but I do always get this error in the terminal:
java -jar /home/haraldur/Desktop/Skolinn/508/Project/target/Project-1.0.jar
Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
try dependency !?
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
I wrote a small decryption tool using BouncyCastle. It works fine if i start it using the console.
java -jar Decryptor.jar
But if i launch it by double clicking the jar file the decryption fails with following exception:
Caused by: java.lang.SecurityException: JCE cannot authenticate the provider BC
at javax.crypto.Cipher.getInstance(Cipher.java:656)
at javax.crypto.Cipher.getInstance(Cipher.java:595)
at logdecryptor.Decryptor.getDecryptedPrivateKey(Decryptor.java:93)
... 60 more
Caused by: java.util.jar.JarException: file:/C:/***/Decryptor.jar has unsigned entries - decryptor/Main.class
at javax.crypto.JarVerifier.verifySingleJar(JarVerifier.java:502)
at javax.crypto.JarVerifier.verifyJars(JarVerifier.java:363)
at javax.crypto.JarVerifier.verify(JarVerifier.java:289)
at javax.crypto.JceSecurity.verifyProviderJar(JceSecurity.java:164)
at javax.crypto.JceSecurity.getVerificationResult(JceSecurity.java:190)
at javax.crypto.Cipher.getInstance(Cipher.java:652)
... 62 more
I am running on Windows 10, non-admin user and im not starting the jar file or the command line using admin permissions. Why does it behave differently depending on how i start it?
Edit:
It's a maven build. Here are my dependencies
<dependency>
<groupId>com.madgag.spongycastle</groupId>
<artifactId>pkix</artifactId>
<version>1.54.0.0</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
and here my plugins
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>decryptor.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
running with goals: clean compile assembly:single
I have built a Java application with Maven in NetBeans. When I built Main project (Run -> build Main project) I got a JAR file called ROVKP_3zad-1.0-SNAPSHOT.jar with only one class called Main.
Then I put that JAR file on Virtual Machine and tried to execute this from command line, from folder where the JAR file is placed:
hadoop jar ROVKP_3zad-1.0-SNAPSHOT.jar com.mycompany.rovkp_3zad.Main
And I got an error saying:
Exception in thread "main" java.lang.ClassNotFoundException: Main
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.apache.hadoop.util.RunJar.run(RunJar.java:214)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
What could be the cause? In my .pom file it is, besides other, defined:
<name>ROVKP_3zad</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
so I think there is not problem with dependencies and classpath. Thank you very much for your help.
Change your POM to:
<name>ROVKP_3zad</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mycompany.rovkp_3zad.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
I currently have a maven project with 1 dependency. When I build it using mvn package and run it I get a ClassNotFoundException for the classes in my dependency. When I export it as a normal jar in Intellij I still get the same error. I have tried just about everything including setting -cp in the command line. Note that the main class is being found, just not the classes in my dep
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.jaredstef</groupId>
<artifactId>hawkeyecountdownbot</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>hawkeyecountdownbot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[4.0,)</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>me.jaredstef.hawkeyecountdownbot.HawkHeart</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
EDIT:
Stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/TwitterException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: twitter4j.TwitterException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
It looks like it's failing to find a dependent class (twitter4j.TwitterException) which is used in a method signature of a class you are using from your own code.
How do you get this compiled without error?
If that works though it may be an issue of incompatible versions of the twitter4j library to be present: one at compile time (with the TwitterException) and a different one at runtime (without the TwitterException).
This may be caused by your allowing to use "4.0 or higher" in the dependency. It may help to define an exact version there to track this down.
When you run a JAR file with the -jar switch, class path defined on the command line (or in environment variables) is ignored.
You will have to add a Class-Path manifest attribute that includes the location of your dependencies.
Maven-Jar-Plugin (or more exactly, the Maven Archiver configuration) supports this by using the addClasspath and classpathPrefix options. Note that you still have to ensure that the dependency libs end up in that directory (e. g. by using maven-assembly-plugin or by manually copying the dependencies there). Or build a fat jar with maven-assembly-plugin that includes all dependencies.
I will suggest you to do following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Main_Class</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>Final_Jar_Name</finalName>