简体   繁体   English

Java Eclipse:作为JAR导出和作为Runnable JAR导出之间的区别

[英]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? 导出为JAR文件和导出为Runnable JAR文件之间的eclipse有什么区别? 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. 可运行的jar包含一个MANIFEST.MF文件,该文件定义了运行jar时要执行的Main类。

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) 不可运行的jar只是类的库,可以添加到类路径中以便重用代码(它还包含清单文件,但没有主类)

A runnable jar is a jar file that has an embedded Manifest file that includes the "Main-Class:" declaration. 可运行的jar是一个jar文件,它有一个包含“Main-Class:”声明的嵌入式Manifest文件。 The "Main-Class" must be defined so the java runtime knows which class to call when the jar is "run." 必须定义“Main-Class”,以便java运行时知道jar“运行”时要调用的类。 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. 如果jar不包含带有“Main-Class:”的清单,则它不被视为“可运行的jar” - 它只是一个Java代码库。

I would guess this is the difference in how Eclipse exports the jar, but not 100% sure. 我猜这是Eclipse导出jar的方式不同,但不是100%肯定。

See this link for more info: http://www.skylit.com/javamethods/faqs/createjar.html 有关详细信息,请参阅此链接: 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. 使用标准JAR文件时,必须在运行jar时在命令行上使用main方法指定类。 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. 使用可运行的JAR,有一个清单文件可以保存该信息,因此您只需键入java -jar myRunnable.jar ,或者只需双击即可。

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 . 在我的情况下,当我拥有所有主类和manifest.mf中指定的所有库路径目录时,我曾经导出为jar。 If many applications are using same library it is unnecessary to export shared library for each jar. 如果许多应用程序使用相同的库,则不必为每个jar导出共享库。 It makes running jar faster. 它使运行jar更快。 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. 但是,很多时候由于不同服务器类路径中的配置问题无法访问库,在这种情况下导出运行缓慢执行文件的runnable jar是有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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