2

Possible Duplicate:
How can I convert my java program to an .exe file ?

I'm trying to export a program written in Java 6 to a JAR file.

My project contains one Java library from the Internet and some Java source files. When I create the JAR file, the classpath should be set by default and the end-user should be able to run the project directly from either the command prompt or some other source.

My goal is to export everything to a JAR file, if that's possible. Also, the program output should be given at the command prompt.

How can I export my program in this way?

Community
  • 1
  • 1
AGeek
  • 5,165
  • 16
  • 56
  • 72

3 Answers3

3

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).

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 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 Apr 13 '09 at 04:48
  • Use the -m option to jar. Read http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html for more details. – C. K. Young Apr 13 '09 at 05:26
2

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

Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71
  • how to use this software... can you provide me brief demonstration.. – AGeek Apr 13 '09 at 05:00
  • 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 Bhangale Apr 13 '09 at 05:11
1

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
  • 73,784
  • 33
  • 194
  • 347