Compiling Java code

Compiling code on the command-line

As discussed on the first slide, all the code is contained in the workspace you specified in the home directory. If you followed the example image, your workspace is a folder called CMSC202 in your home directory.

Browse to your project folder (Lab2), then the source code folder (src). In your source folder should be a folder called lab1.

The example below illustrates how to compile your lab1 code directly from a UNIX terminal.

The first step will be to create a directory called bin, which we do with the following command:

mkdir bin

Next, we use the javac command to compile the contents of the lab1 directory.
javac -d bin lab1/*.java

To execute your code, use the command

java -cp bin lab1.Lab1

or

java bin/lab1.Lab1

-cp is the switch for the setting CLASSPATH, which is the directory where the Java Virtual Machine (JVM) looks for your class files. By default the CLASSPATH is in the current working directory.