Pages

16 June 2009

Run your Java programs in single click

As a Java developer, you might have got a question that why cant we create an
windows executable file for your Java program/application that you had
created. I mean click a file and run your program. Here I am going to explain
some of the methods to create a single executable file for your Java programs.

First approach :

Create an executable jar file. There is a command with which you can create
jar file and run your java programs through them. This command will bundle
your java code (s) and you should give the a class name with in MANIFEST.mf.

Creating an executable JAR file:

Here is the general procedure for creating an executable JAR:

  1. Compile your java code, generating all of the program's class files.
  2. Create a manifest file containing the following 2 lines:
    Manifest-Version: 1.0
    Main-Class: name of class containing main
    The name of the file should end with the .mf suffix. It is important that the file ends with a blank line.
  3. To create the JAR, type the following command:
    jar cmf manifest-file jar-file input-files
    The input-files must include any class files, images, sounds, etc. that your program uses. Optionally, you can include the program's .java files in the JAR. See below for adding directories ot the JAR.
  4. To view the contents of the JAR, type:
    jar tf jar-file
  5. Execute the application from the command line by typing:
    java -jar jar-file
    If the application is GUI-based, you can also launch it by double-clicking the JAR file.
Second Approach:

Creating a batch file. You can create a batch file (*.bat), type in all the commands that are needed to execute your java program.
  • May be you need to move to the directory where your java program has been kept.
  • Say for example if it is in D:/java-workout/ folder, then you have to use
cd d:/java-workout/
  • If you had named your java program as "HelloWorld.java" and compiled it. Then use
java HelloWorld

  • Having got this two commands type it in a test editor and save the file as .bat file.
  • Now clicking on the batch file will run your java program.
Third Approach:

Using a java to exe converter. And coming to this, I like JavaLauncher. I have
tried many java to exe converters. And out of those I found this one as the best
one. It has many options. Given a compiled class file as an input, you can get
an .exe file. Also you can give some icon for your exe file. With this you can
easily distribute your application / software to your customers.


For more information about Java Launcher visit www.syncedit.com

But the major drawback in the second and third approach is, both .bat files and
.exe files are concerned to windows operating system. So if you want to distribute
your application for all the users then the best solution is creating a jar file.



No comments:

Post a Comment