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 (Lab1), then the source code folder (src), and then to the class folder (lab1). In this directory, you should see your source code (Lab1.java).

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

Compile it using the command below (notice the “dot”):

javac -d . Lab1.java

This command will create a new subdirectory named lab1 (the name of the package) and place the file Lab1.class in that subdirectory.

A .class file is a compiled .java file.

To execute your code, use the command

java lab1.Lab1