Compiling and Executing a Java program


A Java program is compiled and executed from the DOS prompt og from Windows ((Note for Windows: The jar extension must be asociated with JAVAW)

Invironment variables PATH and CLASSPATH

In DOS/Windows remember to set the PATH and PATHCLASS invironment variables:

In PATH include the path to the JDK e.g.: PATH=C:\jdk1.3.1_01\bin;

To find the CLASSPATH search for the file TOOLS.JAR. The CLASSPATH should point to the directory where tools.jar is located e.g.


Example of how to compile and run a java program

The source files of the projekt is located in the directory c:\temp\FirstFrame

Each class of a java program must be saved in its own source file, and the file must have the same name as the class.

In this example we have three classes, and therefore three source files:

The FirstFrame class uses the myPanel class, and therefore must be compiled before the myPanel class.

The test class contains the main methods and uses the FisrtFrame class so it must be compiled last.

How to compile

Use the command JAVAC <class> -CLASSPATH c

This how the test class is compiled. Notice that it is necessary to use the CLASSPATH parameter:

c:\temp\FirstFrame>javac -classpath c:\temp\FirstFrame

Important: If you use class'es in a JAR file that is not in the sytems classpath, you must include it in the CLASSPATH statement. E.g, if you want to use class'es in jCO.jar which is located in directory c:\temp\FirstFrame use the statement:

c:\temp\FirstFrame>javac -classpath c:\temp\FirstFrame; c:\temp\FirstFrame\jCO.jar

How to run the program from a DOS prompt

To run a program use this syntax:, remember the CLASSPATH

JAVA -CP <classpath> <class>

This is how the the TEST class is started

c:\temp\FirstFrame>java -cp c:\temp\FirstFrame test

How to create a JAR file

To make a JAR file:

JAR CVF my.jar *.class

You have to indicate which class is the entry point in the JAR file. You are doing this by adding a Main-Class header to the JAR file's manifest. The header takes the form:

Note: Note classes with the name *$1.class is automatically created when you compile. These files must be included in the JAR file.

Main-Class: classname

The header's value, classname, is the name of the class that's the application's entry point.

You first prepare a text file consisting of single line with the Main-Class header and value. For example, if your application was the single-class HelloWorld application, the entry point would of course be the HelloWorld class, and your text file would have this line:

Main-Class: HelloWorld

Assuming your text file was in a file called mainClass, you could merge it into a JAR file's manifest with a command such as this:

jar cmf mainClass app.jar

If you will add the line to the manifest when you creates the jar file use:

jar cmf mainClass my.jar *.class

Note: The name of the manifest file (mainClass) and the jarv file (my.jar) must be in the same order as the m and f parameters in the cmf option.

Running a jar file

A jar file can be run from the DOS prompt by using the command:

java -jar myjarfile.jar

A jar file can also be run from windows


We have the following source files:

The main class is TestMenu java

The files are palced in the directory D:\javatest

Alle operations are performed from the DOS prompt in the D:\Javatestdirectory

1, Compile the source files

JAVAC *.java -CLASSPATH d:\javatest

Now 6 files should have been generated with same name as the files above, but with the extension CLASS

2. Run the application from the DOS prompt

JAVA -CP d:\javatest TestMenu

Note the use of -CP (Classpath)

3. Creating and running a JAR file

A jar file can be run either from the DOS prompt or by doubleclicking the file in Windows (Note: The jar extension must be asociated with JAVAW)

To idnicate which class is the main class, it is necessaru to create a small textfile, that cam be appended to the manifest file.

3.1 Create file to be appended to the manifest file

Create a text file with the text Main-Class: <mainclass>. Note that the class name must not have the class extension, and there should be at least 1 empty line under the statment:

Main-Class: TestMenu

Save this file as a text file named mainClass (You could call it whatever you like)

3.2 Create JAR file

Syntax: JAR <options> <manifest-file> <jar-file> <classes>

jar cvmf mainClass my.jar *.class

This will generate a jar file named my.jar that includes the manifest file mainClass and all classes in the directory.

The options used:

c: Create new archive
v: Verbose output. The verbose output tells you the name of each file as it's added to the JAR file.
m: Include manifest from spcified manifest file
f: Specify archive file name

Note that the manifest and archive file names must be in the same order as the m and f options.


JAR tool commands

To create a JAR file jar cf jar-file input-file(s)
To view the contents of a JAR file jar tf jar-file
To extract the contents of a JAR file jar xf jar-file
To extract specific files from a JAR file jar xf jar-file archived-file(s)
To run an application packaged as a JAR file (version 1.1) jre -cp app.jar MainClass
To run an application packaged as a JAR file (version 1.2 -- requires Main-Classmanifest header) java -jar app.jar
To invoke an applet packaged as a JAR file
<applet code=AppletClassName.class
        width=width height=height>




Converted from CHM to HTML with chm2web Standard 2.7 (unicode)