简体   繁体   中英

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

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?

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)

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM