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>
Related
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>
My app is facing a NoClassDefFoundError when trying to access external dependencies, but only when run as a jar.
Using Intellij, I have a simple app with main class with some calls to external dependencies such as slf4j.
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
///
public static void main(String[] args) {
logger.debug("start");
}
}
The pom.xml includes the relevant dependencies and the app is compiled successfully.
When running the app from the intellij as regular application it is running with no problem.
But when creating an executable jar out of it and trying to run it, it crashes and can't find external dependencies:
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.example.Main.<clinit>(Main.java:18)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Exception in thread "main"
Process finished with exit code 1
I added this to the pom to try to solve the problem, but it didn't help:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<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>com.example.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
The manifest file is located in src/main/resources/META-INF/MANIFEST.MF:
Manifest-Version: 1.0
Main-Class: com.example.Main
It's simple to understand. The jar that you are creating contains only the .class files of your code, not of the other libraries that you're importing. So when you run your code, it is unable to find the external library (org.slf4j in this case) that you have referenced in your code.
I don't know about the maven jar plugin that you're using, but try the maven assembly plugin. This plugin is used to bundle up all the other libraries that you use along with your code and create a "bundled" jar. Add the following to your pom.xml. You can add this portion between <plugins> and </plugins>.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>example</id>
<configuration>
<archive>
<manifest>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
This additionally binds this "assembly" goal to the package phase of maven. So mvn package should create your jar.
Your meta-inf should have following attributes
Manifest-Version: 1.0
Created-By: xxxx
Main-Class: com.sample.Main
ClassPath: path/to/jars/your.jar
java -cp yourpath/example.jar com.sample.Main
Or
java -jar example.jar
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>
I have a maven java project that I am trying to run from the command line. The project is built using Netbeans 8.1. It is built to java-snap-2.0.jar with the maven-dependency-plugin and maven-jar-plugin.
In my root/target directory, I find a lib/ directory containing all the .jar needed for my project's dependencies. I have, for instance, a snap-core-6.0.0-SNAPSHOT.jar which contains, among others, org/esa/snap/core/datamodel/Product.class . In my executable jar, I have a META-INF/MANIFEST.MF file which contains a list of white-space-seperated paths to the jar files in the lib/ directory, including lib/snap-core-6.0.0-20170810.175327-200.jar.
Despite this, when I run the jar file from the command line like so : java -jar java-snap-2.0.jar argument1, argument2 ... argumentN, I get the following error :
Exception in thread "main" java.lang.NoClassDefFoundError: org/esa/snap/core/datamodel/Product
at com.batchprocessing.java.snap.ProcessMultiTemporal.main(ProcessMultiTemporal.java:56)
at com.batchprocessing.java.snap.Main.ProcessMultiTemporalHPC(Main.java:178)
at com.batchprocessing.java.snap.Main.main(Main.java:189)
Caused by: java.lang.ClassNotFoundException: org.esa.snap.core.datamodel.Product
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
Here is an excerpt from the pom.xml file :
(...)
<build>
<plugins>
<!-- Copy dependencies during package phase to root/lib directory -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<!-- Build an executable JAR and add classpaths (in lib/) to manifest -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.batchprocessing.java.snap.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
(... other plugins ...)
</build>
(...)
I would greatly appreciate help figuring this out. I've been using this application for a year by running it from the IDE, but I'd like to be able to run it from the command line and move it to other machines (by moving the executable jar and the lib/ directory). I run into other issues if I try using a jar-with-dependencies or shade approach, so I'd like to get this setup (maven-jar-plugin + maven-dependency-plugin) working.
Alright, the issue (as rightly identified by Roman Pushkovskiy) was that the names of the jar files in the manifest file were different from their names in the lib directory. These dependencies are mostly snapshots, and so the name of the jar is something like dependency-1.0.0-SNAPSHOT.jar. In the manifest, they would be attributed a unique name based on the date of the snapshot : lib/dependency-1.0.0-20170810.175327-200.jar. The solution was to add this line to the maven-jar-plugin : <useUniqueVersions>false</useUniqueVersions>.
Updated pom.xml excerpt :
(...)
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.batchprocessing.java.snap.Main</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>
(... other plugins ...)
</build>
(...)
Hope this can help others !
I have create a maven project. It is running fine. Now i want to run my code through jar. After maven build i got a jar file in .m2 folder. When I try to run this jar using
java -jar "jar path"
getting no main manifest attribute, in "jar path".
My POM.xml is
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>main.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Please suggest how to get over rid of the problem.
Now i want to run my code through jar
exec-maven-plugin is for executing a program during a maven build.
You don't want it.
And as a side note, you don't have specified any goal for it.
So, it will do nothing.
You want to package your jar in a way that it is executable.
So use rather the maven-jar-plugin :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>main.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
To create a JAR with dependencies specified in the pom, instead of, use the maven-assembly-plugin with the jar-with-dependencies descriptorRef:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>main.Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>