views:

556

answers:

3

Im trying to export a program written in java (JDK 1.6.0) to a .jar file.

To my project, I have added one Java library(which was available on internet) and some Java source files. So when i create the jar file, the classpath should be set by default and the person should be able to run the project directly either from command prompt or through some other source.

My will is to export everything to a jar file, if its possible..

Also, the ouput is given at command prompt, please guide how is it possible..

+1  A: 

You can use Jsmooth http://jsmooth.sourceforge.net/ to bundle your java code to make an executable.

Bhushan
how to use this software... can you provide me brief demonstration..
AGeek
It very easy, read http://jsmooth.sourceforge.net/docs/jsmooth-doc.html. Simply download and install and run. The gui will direct you with context help on each item.
Bhushan
+3  A: 

You need to write a META-INF/MANIFEST.MF file for this; the file should sit within the jar file you're distributing (i.e., the one that people use with java -jar). Here's an example for a recent "project" I wrote:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 11.3-b02 (Sun Microsystems Inc.)
Main-Class: com.hedgee.simonsays.Main
Class-Path: lib/libthrift.jar

The main fields to care about are Main-Class (which specifies which class to look for your main method), and Class-Path (which specifies the external jar files you're using).

Chris Jester-Young
can u please give me an example to above code.. how should i jar my project files and library file.. is there any software which cn do this.
AGeek
Use the -m option to jar. Read http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html for more details.
Chris Jester-Young
+1  A: 

I have had good experiences with two different approaches:

1) Use the fatjar plugin with eclipse to create a single jar file with all the jars you need embedded (uses a custom classloader). This creates a clickable jar file (if it is a GUI program). If you need console output for this Jar wrap it with jsmooth as described by Bhushan.

2) Use the "Export -> Runnable Jar" in Eclipse 3.5 Milestone 6 with the "copy dependent libraries to a subfolder" option to create an ordinary runnable jar with a proper manifest referencing the dependent jar files (which are put in a subfolder). This works very well if you want a solution not using any tricks, but it is a bit hard to script.

Thorbjørn Ravn Andersen