ECCS 166 – Programming 3
Spring Quarter 2005 – Dr. Estell
Creating Executable JAR Files for Java Application Programs
Originally, the only way that one could execute a Java program over the Internet was to distribute the code as an applet
attached to a web page. There are shortcomings to this method, most notably the "sandbox" approach used by browsers
to protect clients from malicious code, thereby limiting potential functionality. Executable JAR (for Java Archive file
format) files enables developers to bundle multiple files into a single archive in such a way that the application or applet
contained therein can be readily launched. This handout presents the basic steps for creating an executable JAR file;
students desiring additional information on JAR files can visit http://java.sun.com/docs/books/tutorial/jar/ for more
information.
In order to get this to work, one needs (a) a program and (b) an appropriately organized directory structure. The program we will used
in this example is called "GuessingGame" where you try to guess a value between 0 and 99. Ideally, whenever you write a program
you are placing it within its own folder, so that all of the related files are kept together, yet separate from the files for other programs. All
of the files for this program are in a folder called "GuessingGame". Within this folder there is a folder called "src" (short for source
code) where we find the following Java application file:
C:\Java\Apps\GuessingGame\src>type GuessingGame.java
// GuessingGame.java - John K. Estell - 10 March 2004
// A Java application that allows you to guess a number...
package src;
import javax.swing.JOptionPane;
import java.util.*;
//
//
//
//
place this line before anything else in your source code file
package name should match directory name
for our dialog boxes
for the Random class
public class GuessingGame {
// main: needed for all applications...
public static void main( String args[] ) {
// variables of our method - not exactly an OOP way to go...
String playerInput;
// input string from the user
int playerGuess;
// value of integer parsed from the input
int numberOfGuesses = 0;
String result = "";
// create a random object and pick a value between 0 and 99, inclusive
Random r = new Random();
int theAnswer = r.nextInt( 100 );
// sit in an infinite loop until the correct answer is entered
while ( true ) {
numberOfGuesses++;
playerInput = JOptionPane.showInputDialog( result + " Enter a guess between 0 and 99:" );
playerGuess = Integer.parseInt( playerInput );
if ( playerGuess == theAnswer )
break; // as the correct answer has been guessed
// if here, then provide feedback on whether the guess was high or low
if ( playerGuess < theAnswer )
result = "Your guess of " + playerGuess + " is too low.";
else
result = "Your guess of " + playerGuess + " is too high.";
}
// display the final results to the user
result =
"Your guess of " + playerGuess + " is correct!\n"
+ "It took you " + numberOfGuesses + " guesses.";
JOptionPane.showMessageDialog( null, result, "Your Results",
JOptionPane.INFORMATION_MESSAGE );
// terminate the execution of the application
System.exit( 0 );
}
}
We will compile the application from the command line:
C:\Java\Apps\GuessingGame\src>javac GuessingGame.java
Hopefully, all we get back is the command prompt. In this case, no news is good news: the application was compiled
successfully. Looking at the contents of the src directory, we now have a .class bytecode file:
C:\Java\Apps\GuessingGame\src>dir
Volume in drive C has no label.
Volume Serial Number is 3846-0589
Directory of C:\Java\Apps\GuessingGame\src
02/22/2005
02/22/2005
02/22/2005
02/22/2005
11:43 AM
<DIR>
.
11:43 AM
<DIR>
..
11:45 AM
1,239 GuessingGame.class
11:43 AM
1,866 GuessingGame.java
2 File(s)
3,105 bytes
2 Dir(s) 72,418,263,040 bytes free
Go up one level in the directory structure to your directory for the program:
C:\Java\Apps\GuessingGame\src>cd ..
We now need to create a manifest file that specifies where the application's entry point (i.e. the main() method for a Java
application) is located. Manifest files are created automatically whenever a JAR file is created; however, in order to create
an executable JAR file, we need to modify the manifest file. We first create a text file called mainClass.txt that
contains the following:
C:\Java\Apps\GuessingGame>type mainClass.txt
Main-Class: src.GuessingGame
Please make sure that there is a carriage return at the end of the line; otherwise, an error may occur. Next, we create the
JAR file using the basic command format:
jar cmf manifest-addition jar-file input-file(s)
The options and arguments used in this command are as follows:
•
•
•
•
•
•
The c option indicates that you want to create a JAR file.
The m option indicates that you want to merge information from an existing file into the manifest file of the JAR file
you're creating.
The f option indicates that you want the output to go to a file (the JAR file you're creating) rather than to standard
output.
manifest-addition is the name (or path and name) of the existing text file whose contents you want included
in the JAR file's manifest.
jar-file is the name that you want the resulting JAR file to have.
The input-file(s) argument is a space-separated list of one or more files or directories that you want to be
placed in your JAR file.
For our application, we enter the following:
C:\Java\Apps\GuessingGame>jar cmf mainClass.txt guessinggame.jar src
Please note that, by placing all of our source code and class files in the src directory, we do not have to explicitly list all of
the input files.
The application can now be executed in one of three ways:
1. Via the command line: java –jar guessinggame.jar
2. Via Windows Explorer by double-clicking on the icon for the JAR file:
3. Via the Internet by clicking on the appropriate link on a web page. The following dialog box results when using
the FireFox browser:
Just open the file using the default jarfile and click on OK.
And now, here's our exciting Java application in action:
Skipping a few iterations…
From this example, we can see that it is relatively easy to create an executable JAR file for distribution over the Internet.
© Copyright 2026 Paperzz