OOPortal OOPortal

Structured Programming   «Prev  Next»

Compile Java Program - Exercise

The Java compiler

Compile the HelloWorld program (Unix)
Objective: Compile the HelloWorld program.
This exercise is not scored. It's an opportunity for you to check your understanding of the material covered in the preceding lesson. When you're finished, click the Submit button to receive credit for having completed it.
In this exercise you will compile your first Java program. You'll be provided with the source code for a program named HelloWorld that displays the message "Hello World!" It's become somewhat traditional for your first program in any new language to perform this task.
To write your Java programs you can use any text editor you like. In this course, a simple text editor such as Vi or Emacs will work fine. If you're interested in text editors with more advanced features that are specifically suited for writing source code, please refer to the Resources section for a few recommendations.
  1. Open your text editor and enter the following Java source code exactly as it appears.
    class HelloWorld {
      public static void main(String[] args) {
        System.out.println("Hello World!");
Java is a case-sensitive program language; thus, it's very important not to change the case of any of the above source code.

At this point, most if not all of this program probably seems somewhat puzzling. That's to be expected. In this lesson the primary objective is to use the compiler. A detailed explanation of the program follows a little later in this module.
  1. Save the source code in a file named HelloWorld.java in your progfun directory.
  2. Open a shell window and navigate to your progfun directory. List the contents of this directory to confirm the existence of the file HelloWorld.java.
  3. The command to run the Java compiler is javac. Compile your program by issuing the following command at the command prompt.
    javac HelloWorld.java

If all goes well, no error messages will be displayed and you will simply see a new command prompt. To confirm that HelloWorld.java has compiled successfully, list the contents of your progfun directory to see that a new file has been created named HelloWorld.class. This file contains the bytecode produced by the compiler.