1
votes

Below is my Jar structure. This is standalone jar.

MyApp.jar
--> .settings
--> com
--> lib
--> META-INF
--> resources
--> .classpath
--> .project

I am attempting to execute this jar file from bat file.

I have mentioned classpath and main class in MANIFEST.MF file under META-INF folder from MyApp.jar

Manifest-Version: 1.0
Class-Path: ./lib/jar1.jar ./lib/jar2.jar ./lib/jar3.jar
Main-Class: com.bank.Main

Inside my bat file: java -jar D:\app\MyApp.jar

Is it correct way to lib folder inside jar?

But still i am facing ClassNotFoundException.

Exception in thread "main" java.lang.NoClassDefFoundError: javax/jms/JMSException

Anything i missed out Please advise me.

2
Please add the full error message. - Fildor
Exception in thread "main" java.lang.NoClassDefFoundError: javax/jms/JMSException - deadend
Seems like you are missing a dependency jar? - Fildor
No.. This is my MANIFEST.MF.. Manifest-Version: 1.0 Class-Path: ./lib/javax.jms.jar Main-Class: com.bank.Main - deadend
When you start it from cmd, (without bat) does the same error occur? - Fildor

2 Answers

2
votes

Put the lib folder outside of your current main jar [MyApp.jar] directory and execute it.

You have packaged the jar dependency inside your main jar. The intention of Class-Path is to add an external jar to the classpath, with the path relative to the location of the main jar.

Packaging a jar within a jar is not supported by standard Java classloaders. If you want, you can explode the inner jar into the main jar, though. Maven can do this for you.

1
votes

Fat jar is not supported in Java by default. There are two options.

  1. You can define a custom class loader which has to load jars inside lib directory programmatically.

  2. You can manually merge all packages of jars inside lib directory to make a one jar and run it. Refer [https://dzone.com/articles/java-8-how-to-create-executable-fatjar-without-ide ]