Computer Science Department
Programming Methods Laboratory

Compilation     winter 00/01
Ecole Polytechnique Federale de Lausanne
How to Compile and Run Java Programs

As you may already know, Java organizes a program into classes, with each class in a program compiled into a separate binary object code file (.class files).

It is good practice to separate the source code (in .java files) from the Java classfiles. A good way to do it is to create in your compilation directory a sources directory and a classes directory.

The directory in which source files (.java files) should be stored is compilation/sources/j0.
The directory into which class files (.class files) are compiled is compilation/classes.

javac is the Java compiler that you will use during the project.


By default, javac places the class files it generates in the same directory as the corresponding source file. To override this behavior, use the -d option.

The -d directory option will make the specified directory the root of the class hierarchy. All the .class files are then placed in this directory, or in the appropriate subdirectory below it, depending on the package name of the class.


    javac -d ~/compilation/classes ~/compilation/sources/j0/
creates a classfile for the sourcefile in the directory compilation/classes.

When the -d option is specified, javac automatically creates any directories it needs to store its class files in the appropriate place.


When a source file references a class, javac searches for the definition of that class using the class path. To specify the path javac uses to look up classes, use the -classpath path option.


    javac -classpath ~/compilation/classes -d ~/compilation/classes ~/compilation/sources/j0/
creates a classfile Titi.class somewhere in the directory compilation/classes and looks up classes referenced in in the directory compilation/classes. If you are too lazy to write the -classpath option all the time, you can also set the environment variable CLASSPATH to your classes directory. For tcsh this would look like this:
    setenv CLASSPATH ~/compilation/classes


The java command executes Java programs. The path that java uses to look up the specified class and all other classes that it loads can be set by using the -classpath path option (if the classpath was not set already by the CLASSPATH variable).


    java -classpath ~/compilation/classes j0.Titi
runs the main() method of the Titi class. The interpreter looks for all the classes in the directory compilation/classes.


If you are used to debug programs with a source-level debugger, then you should try out jdbg. It is used like java but you also have to provide the path to your sources, so that the debugger can find your sourcefiles.


    ~czenger/jpdg/bin/jpdg -classpath ~/compilation/classes -sourcepath ~/compilation/sources j0.Titi
runs the main() method of the Titi class. The interpreter looks for all the classes in the directory compilation/classes.

Don't forget to read the javac documentation on the pages.