3

I used eclipse to create executable jar. It relies external other jars.
In Eclipse, It is simple that you just need to choose Extract required libraries into generated JAR.
You can create an executable jar. It can be executed any places where jre is installed.

But If I use command line to compile jar.
javac -classpath [external jars] *.java
jar cfm [a name].jar manifest *.class [external jars]

It can generate jar. But the jar can only be executed in the directory where it is produced. If I put it into another directory or machine, it complains NoClassDefFoundError.

So, my question is that how I can generate executable jar using command line as Eclipse.

chnet
  • 1,993
  • 9
  • 36
  • 51
  • Did you try selecting package required libraries into generated jar instead of extract? – MBU Nov 16 '11 at 21:42
  • I do not have any problem with Eclipse. The jar generated by Eclipse can be executed in any place. My problem is that how to do this using command line. – chnet Nov 16 '11 at 21:48
  • Have you opened the jar file with winrar or something like that to check if the other jars are packed inside? – Tudor Nov 16 '11 at 22:01

3 Answers3

3

A jar file cannot have its dependency jars inside. In case of Eclipse, it will unpack all the classes from the dependency jars and will bundle it into your single jar along with your class files. If not in the eclipse way, you need to

1) Create a manifest file which lists all the dependency jars

Manifest-Version: 1.0
Main-Class: Your Main class
Class-Path: dependency1.jar dependency2.jar dependency3.jar
 dependency4.jar dependency5.jar

2) Create your jar with your class files using the class path including all the dependency jars and using the above created mainfest file.

3) In this same folder where you created your jar, place all the dependency jars.

Now your folder will look like this,

yourjar.jar (With the manifest file you created above) dependency1.jar dependency2.jar dependency3.jar dependency4.jar dependency5.jar

4) Now if you want to share this, you need to share this folder and you can launch your jar from this folder. This is your executable folder and you can run it from anywhere.

Marimuthu Madasamy
  • 13,126
  • 4
  • 30
  • 52
  • Yes. I used your method. I need to share the folder and launch jar from the folder. But If I generate jar using Eclipse, I do not need the folder. I can place the jar (not folder) in any places. – chnet Nov 16 '11 at 22:25
  • With the single jar, if there is any updates on any of the dependency jars, you need to bundle your whole jar again; you can't simply build and replace the modified dependency jar. With this approach, you can just update the modified jars. – Marimuthu Madasamy Nov 16 '11 at 22:38
2

Eclipse use Ant to package jar file, you can save the ant script that eclipse use to generate the jar checking the checkbox Save Ant File in the export window :

screenshot

so, you can generate the Ant Build.xml script and then execute it using ant directly from the command line without using eclipse anymore if you want.

aleroot
  • 71,077
  • 30
  • 176
  • 213
0

My preferred method for creating an executable jar is to use a utility called one-jar. I have a blog post discussing how to use it in maven and ant: my one jar blog post

Chris J
  • 9,164
  • 7
  • 40
  • 39
  • The URL now gives a 404 error. Were you referring to [this one](http://chrisjordan.ca/post/15052417887/building-self-contained-executable-jars-2-ways)? – David Pärsson Nov 05 '12 at 19:32