The following simple program prints the String output
rules!and stops:
We will explain the various syntactic constructions used in this sample program later in the monograph.class Motto { public static void main(String[] args) { System.out.println("Java rules!"); } }
Before this program can be executed, it must be compiled into a form suitable for machine execution called a class file Motto.class. The name of the class file generated for a class C is simply C.class.
In DrJava, Java class definitions are entered as program text in the Definitions window and compiled using the Compile button at the top of the window. This process produces a class file for each class following the usual naming convention, but class files are placed in a special directory that is not accessible to the programmer. In DrJava, a programmer is only concerned with program source code; the class files are invisible.
If a compiled Java class C contains a main method with the header
then it can be executed as a program in DrJava by entering the commandpublic static void main(String[] args) {
java Cin the Interactions window. Hence, after the Motto class has been entered in the Definitions window and successfully compiled using the Compile button, it can executed by entering the command
java Mottoin the Interactions window. Of course, evaluating the expression
Motto.main(null);in the Interactions window will produce exactly the same results.
Finger Exercise: Using DrJava, define the Motto class
given above and execute it as program.