Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Eclipse export my entire project as runnable JAR file?

Tags:

java

eclipse

jar

I have a Java project called Project1, containing some ten packages. In package3, I have a class called MyPrgm which I want to export as a runnable jar. This is fine and I can execute it in the command prompt / using a batch file.
However, when I open MyPrgrm.jar archive using WinRAR I realize that every single class in Project1 is in the archive; is there a way to export only the classes that are required for the program to run? I am using Eclipse IDE.

like image 641
jII Avatar asked Jun 14 '12 12:06

jII


People also ask

What is the difference between runnable jar and executable jar?

FYI: In simple terms, the difference between a JAR file and a Runnable JAR is that while a JAR file is a Java application which requires a command line to run, a runnable JAR file can be directly executed by double clicking it.

What is runnable JAR file?

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.

What is launch configuration runnable jar Eclipse?

Launch configurations are presumably used because they describe the main class you want to run, and the libraries the main class needs. They are created automatically when you run your main class inside Eclipse.


2 Answers

It does this because you told it to. If you only want to export one package, then only export one package. For instance, if you right click on the project and press Export, Eclipse will try to export everything in the project, but you can fine tune this easily via the export wizard. If you right click on a package and click Export, then Eclipse will try to export only that package.

In particular look at the "Jar File Specification" portion of the Export wizard where you can pick and choose what to export.

like image 194
Hovercraft Full Of Eels Avatar answered Nov 15 '22 13:11

Hovercraft Full Of Eels


If you don't want all the classes in the JAR, then choose export only for that particular package, not for the entire project.

Even if the above doesn't work (no reason why it shouldn't), you can still remove the additional classes from the executable. JAR is basically a ZIP archive, you can open it with any archive manager and add/remove files as you wish.

like image 23
Radu Murzea Avatar answered Nov 15 '22 12:11

Radu Murzea