Distribution of Java Applications

Exe Files for Microsoft Windows

It's possible to make exe files for Windows. However, these programs can only use JDK1.1. That means, they cannot use e.g. Swing or RMI.

A detailed instruction  

Platform Independent Distribution (JAR files)

To understand the creation of jar files, we have to know:

What do we need to run a Java program?

A Java program consists of several class files with byte codes. We use classes we've made ourselves, and we use classes from the Java API and other packages.

The byte codes are platform independent. To run the code on a special platform (Microsoft, Linux, Mac), we need a Java interpreter especially made for that platform.

There is a product named  Java 2 Runtime Environment (JRE). This product consist of the Java API and a Java interpreter. (When we create our ordinary Java programs, we use SDK2 which contains much more, e.g. the appletviewer and the javac compiler.)

As a conclusion, a user running a Java program needs two things:

1. The class files and supporting packages. We pack them in a jar file, se below.
2. Java 2 Runtime Environment. We may distribute this product together with the jar file. This is not shown below. (JRE is allowed to redistribute, as opposed to SDK2.) Details about JRE downloading and licensing. (This version of JRE is not the same as that  enclosed in the SDK2 distribution pack, which you'll find in the jdk1.4 directory.)

Packing class files into an executable Java Archive (jar) file

A jar file is created by the jar tool, which is a part of the SDK2. The files are packed in a format readable by WinZip, that means, you may use WinZip to look at the contents of a jar file.

A jar file is an archive file with some addendums. Here we look at the addendum for an "executable jar file". Other sorts of addendums, take a look  here

A receipt to create a simple executable jar file:

1. All the class files should be located in the same directory, e.g. myJarExample. No other class files in this directory!

2. We must in some way inform about where the starting point of the program is, that means, we have to state the name of the class with the correct main() method. Assume main() is in a class named HelloWorld. Then the following line is inserted in a text file (remember to press the Enter key after the line):

            Main-Class: HelloWorld

This text file is named, e.g.  start.txt. This file should be placed in the same directory as the class files.

3. The jar file is created with the following command from the command prompt  (* means all class files in the directory):

            >jar cmf start.txt myJar.jar *

The jar file is now myJar.jar.

What if the jar file should contain packages?

As an example, we use the Renovation case in chapter 12. In addition to the ordinary Java files, this application uses the myLibrary package. Let the name of the directory be Renovation. Then you have to:

A: Copy the whole myLibrary directory into the Renovation directory. (Remember, the myLibrary classes have to be compiled.) Then the Renovation directory has one subdirectory:  myLibrary with class files

B: Insert the following line in a start.txt file:

            Main-Class: RenovationChap12

C: Run the jar tool:

         >jar cmf start.txt  myJar.jar  *.class  myLibrary/*.class 

The jar  file is, as before, named myJar.jar

Running an executable jar file

You may run the jar file from the command prompt:

        >java -jar myJar.jar

Or you may double-click on the file in Windows Explorer. 

Java 2 Runtime Environment has to be installed on the computer for this to work

Jar files in classpath

The jar file may be part of the classpath environment variable.

An example: Our jar file myJar.jar is located in the directory c:\java\myPrograms\myJarExample. 

If the classpath is expanded with  c:\java\myPrograms\myJarExample\myJar.jar, (note that the file name has to be part of the classpath), the user may run the program in this way:

        >java HelloWorld

The user may use the classes from the jar file in her own programs, as well.

At page 620 in the book, we use a zip file (a database driver) in the same way.