Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

eclipse

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?

like image 202
well actually Avatar asked Feb 11 '11 22:02

well actually


People also ask

Is a jar the same as an executable?

An Exe file is an executable file that can be executed in Microsoft OS environment. Jar file is container of Java Class files, including other resources related to the project. Jar file can be executed only if Java run time environment.

How do I export a Java project as runnable jar in Eclipse?

To export your project, right-click it and select Export. Select Java > Runnable JAR file as the export destination and click Next. On the next page, specify the name and path of the JAR file to create and select the Launch configuration that includes the project name and the name of the test class.

Are jar files runnable?

A runnable jar file allows a use to run Java classes without having to know class names and type them in a command prompt, rather the user can just double click on the jar file and the program will fire up. A runnable jar allows Java classes to be loaded just like when a user clicks an exe file.


2 Answers

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)

like image 78
Bozho Avatar answered Sep 22 '22 10:09

Bozho


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

like image 23
Andy White Avatar answered Sep 25 '22 10:09

Andy White