This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Executable java file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Executable java file" Watch "Executable java file" New topic

Executable java file

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Could you please tell me how to create an executable file for a java appliction.
step by step instruction are much appriciated.

SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Paul Zill

Joined: Jun 12, 2003
Posts: 28
can you be a bit more specific?
if you just want to run a java app instead of typing it from the command line just create a shortcut and for target put in "<path to java>java <path to class file>TestApplication"
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
I mean if I want to give this the program to a person who doesn't know java. I should give him with an executable file. so that he can double click the icon and get the program to run
hope this is more specific
Paul Zill

Joined: Jun 12, 2003
Posts: 28
well then . . .
in windows you can create a batch file (.bat) which does the same thing as the shortcut.
The only problem would be is if the person has java installed and their environment variables set (like a JAVA_HOME, or the jdks' bin\ directory in the path).
You could circumvent this by also including the jdk with your application (it's about 46 mb for 1.4) and calling that one explicitly in your batch file.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Another possibility (more common) is the executable .jar file. An executable .jar file is simply a .jar file with an extra line in the manifest:
Main-Class: my.main.ClassName
When you double click on this .jar file, the applciation should launch(*)
However, an executable .jar file IGNORES the system CLASSPATH variable. You can specify a Class path in the manifest:
Class-Path: anotherJarFile.jar ..
Note the capitalization and hyphenation of "Class-Path" Also note that the elements listed are relative to the location of the .jar file and not relative to the current directory or anything else. Also note that the different elements are separated by spaces, not : or ;
(*) This is, of course, assuming that the JRE is correctly installed. Just include the distribution .exe with the application, and tell the client that if they don't have java already installed on the target machine, they will have to install it before running their program. I usually have the client open up a DOS prompt (or the equivelent) and type java -version. This double checks that java is installed and enables you/the user to verify that the required version is installed. There are ways to automate this for "dumb" users, but I gave my mother these instructions and she could follow them....
One other note--an executable jar file is defaulted to run with the javaw command, not the java command. This means that you won't get a DOS prompt/whatever upon double clicking, and any System.out.prinln() messages (or the equivelent) will not show up.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Thanks for the answers....
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Executable java file
It's not a secret anymore!