Java Eclipse: Difference between exporting as a JAR and exporting as a Runnable JAR

JavaEclipseJar

Java Problem Overview


What is the difference in eclipse between exporting as a JAR file and exporting as a Runnable JAR file? Aren't they both runnable? What are the pros/cons of each?

Java Solutions


Solution 1 - Java

The runnable jar contains a MANIFEST.MF file, which defines the Main class to be executed when the jar is run.

Non-runnable jars are just libraries of classes, that can be added to the classpath so that code is reused (it also contains the manifest file, but no main class there)

Solution 2 - Java

A runnable jar is a jar file that has an embedded Manifest file that includes the "Main-Class:" declaration. The "Main-Class" must be defined so the java runtime knows which class to call when the jar is "run." If a jar does not include a manifest with the "Main-Class:" it is not considered a "runnable jar" - it is just a library of Java code.

I would guess this is the difference in how Eclipse exports the jar, but not 100% sure.

See this link for more info: http://www.skylit.com/javamethods/faqs/createjar.html

Solution 3 - Java

With the standard JAR file, you have to specify the class with the main method on the command line when running the jar. With a runnable JAR, there is a manifest file that will hold that information so you can just type java -jar myRunnable.jar, or simply double click it.

Solution 4 - Java

In my case, I used to export as a jar when I had all main class and all the libraries path directory specified in the manifest.mf . If many applications are using same library it is unnecessary to export shared library for each jar. It makes running jar faster. But, many times due to configuration issue in different server class-path cannot access library and in that case it makes sense to export the runnable jar that makes file slow to execute and large.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwell actuallyView Question on Stackoverflow
Solution 1 - JavaBozhoView Answer on Stackoverflow
Solution 2 - JavaAndy WhiteView Answer on Stackoverflow
Solution 3 - JavaEric-KarlView Answer on Stackoverflow
Solution 4 - JavaMR ANDView Answer on Stackoverflow