1

I am trying to build an executable jar. When i execute the code from the project via eclipse, it runs fine, but not when the jar is executed from CMD line. Looks like some issue with class path, but i am not sure ...I have the following dependencies in my pom file:

<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.12</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> <scope>compile</scope> </dependency> 

and the following maven plugins in the tag of the pom file:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>org.swx.nursing.tools.quicklaunch.executor.QuickLaunch</mainClass> </manifest> </archive> </configuration> </plugin> 

Then I use the following to build the jar: mvn clean install

When i execute the jar, i see the following message i am not sure why:

C:\Workspaces\CCQueryHotkey\quicklaunch\target>java -jar quicklaunch-0.0.1-SNAPSHOT.jar Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory at org.swx.nursing.tools.quicklaunch.executor.QuickLaunch.<clinit>(QuickLaunch.java:18) Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ... 1 more 

I see that the SLF4j jars are in the project folder pulled by maven.

Please advise!

Thanks

3 Answers 3

1

You can use the shade plugin to create an executable jar. Look at this link for example

Sign up to request clarification or add additional context in comments.

Comments

0

You will have to set classpath to your .m2 repository. Try to execute

java -cp "Path to your .m2 repository." -jar quicklaunch-0.0.1-SNAPSHOT.jar 

1 Comment

That did not work, it gave me a message saying that the main class is missing. Also i am trying to build an executable jar, not just run it from CMD
0

You are getting java.lang.NoClassDefFoundError because classes are not available during runtime at classpath location.

You can use maven assembly plugin to fix the issue.

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <!-- nothing here --> </plugin> <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> <finalName>test</finalName> <appendAssemblyId>false</appendAssemblyId> <archive> <manifest> <mainClass>org.swx.nursing.tools.quicklaunch.executor.QuickLaunch</mainClass> </manifest> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> 

Update your pom.xml with the configuration given above and run mvn clean install. Then, cd into the target directory and try again:

java -jar test.jar

For reference: maven assembly plugin

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.