Structured Programming   «Prev 

The Java compiler

Compile HelloWorld (Windows)

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 will 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 that you like, but the instructions for this exercise show how to use Notepad, a very simple general-purpose text editor for Windows. If you are 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 the Notepad editor by going to the Start menu, selecting Programs, selecting Accessories, and then selecting Notepad. The menu navigation to Notepad may be a little different on your computer.

Screen shot of Start -> Programs -> Accessories -> Notepad
  1. In Notepad enter the following Java source code exactly as it appears. The simplest approach is to copy and paste the code directly from this page into Notepad.
    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 in your progfun directory using File > Save As.... In the Save As dialog specify the name of the file within double quotes. (Notepad has a tendency to add an extension of .txt to file names. Specifying the name of the file within double quotes avoids this problem.)

Screen shot of Save As dialog with "".

  1. Open a Command Prompt window and navigate to your progfun directory. List the contents of this directory to confirm the existence of the file Here is what you should observe.

Screen shot of code string

C:\>cd c:\progfun
Volume in drive C has no label.
Volume Serial Number is 4C26-5631
Directory of C:\progfun
02/13/01  02:29p     <DIR>       .
02/13/01  02:29p     <DIR>       ..
02/13/01  02:31p           115
3 File(s)    115 bytes
2,713,687,040 bytes free
  1. The command to run the Java compiler is javac. Compile your program by issuing the following command at the command prompt. javac

If all goes well, no error messages will be displayed and you will simply see a new command prompt. To confirm that 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.
Screen shot of code string

Volume in drive C has no label.
Volume Serial Number is 4C26-5631
Directory of C:\progfun
02/13/01  02:43p     <DIR>     .
02/13/01  02:43p     <DIR>     ..
02/13/01  02:43p           426 HelloWorld.class
02/13/01  02:31p           115
4 File(s)        541 bytes
2,713,604,096 bytes free

If you run into any problems when compiling this program, first double check to make sure that you've copied the supplied code exactly. If the code appears to be fine, please contact your tutor, and in your message include any error messages you have observed along with the code in your file.