JavaEclipse: 导出为 JAR 和导出为 Runnable JAR 之间的区别

作为 JAR 文件导出和作为 Runnable JAR 文件导出在 Eclipse 中的区别是什么?他们不都能跑吗?每种方法的优缺点是什么?

61587 次浏览

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)

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.

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

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.