The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Getting Started
Lesson: The "Hello World" Application

"Hello World" for Microsoft Windows

It's time to write your first application! These detailed instructions are for users of Microsoft Windows platforms, which include Windows XP, 2000, ME, and 98. Instructions for other platforms are in "Hello World" for UNIX (in the Getting Started trail) and "Hello World" for Mac OS X (in the Getting Started trail).


A Checklist 

To write your first program, you need:
  1. The J2SE TM Development Kit 5.0 (JDK TM 5.0). You can download the Windows version now (outside of the tutorial). (Make sure you download the JDK, not the JRE.) Consult the installation instructions (outside of the tutorial).
  2. A text editor. In this example, we'll use NotePad, a simple editor included with the Windows platforms. You can easily adapt these instructions if you use a different text editor.

These two items are all you need to write your first application.


Note: Consider using an Integrated Development Environment (IDE) to help write your programs. JDK 5.0 is available bundled with the NetBeans IDE. You can download this bundle from the JDK 5.0 download page (outside of the tutorial).

Creating Your First Application

Your first application, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will: 

top


Create a Source File

To create a source file, you have two options:

First, start your editor. You can launch the NotePad editor from the Start menu by selecting Programs > Accessories > NotePad. In a new document, type in the following code:

/**
 * The HelloWorldApp class implements an application that
 * simply displays "Hello World!" to the standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        //Display "Hello World!"
        System.out.println("Hello World!");
    }
}
 Be Careful When You Type

Type all code, commands, and file names exactly as shown. Both the compiler (javac) and launcher (java) are case-sensitive, so you must capitalize consistently.

HelloWorldApp helloworldapp

Save the code in a file with the name HelloWorldApp.java. To do this in Notepad, first choose the File > Save As menu item. Then, in the Save As dialog box:

  1. Using the Save in combo box, specify the folder (directory) where you'll save your file. In this example, the directory is java on the C drive.
  2. In the File name text field, type "HelloWorldApp.java", including the quotation marks.
  3. From the Save as type combo box, choose Text Documents (*.txt).
  4. In the Encoding combo box, leave the encoding as ANSI.
When you're finished, the dialog box should look like this.

TEXT The Save As dialog, as described in the text.

The Save As dialog just before you click Save.

Now click Save, and exit NotePad.

top

Compile the Source File

Bring up a shell, or "command," window. You can do this from the Start menu by choosing MS-DOS Prompt (Windows 95/98) or Command Prompt (Windows NT/XP), or by choosing Run... and then entering cmd. The shell window should look similar to the following figure.

a window where you can enter DOS commands

A shell window.

The prompt shows your current directory. When you bring up the prompt, your current directory is usually C:\WINDOWS for Windows 98, C:\WINNT for Windows NT, or your home directory for Windows XP (as shown in the preceding figure).

To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is java on the C drive, type the following command at the prompt and press Enter:

cd C:\java

Now the prompt should change to C:\java>.


Note: To change to a directory on a different drive, you must type an extra command: the name of the drive. For example, to change to the java directory on the D drive, you must enter D:, as shown in the following figure.

Changing directory on an alternate drive

Changing directory on an alternate drive.


If you enter dir at the prompt, you should see your source file, as the following figure shows.

Directory listing, showing the .java source file.

Directory listing, showing the .java source file.

Now you are ready to compile. At the prompt, type the following command and press Enter.

javac HelloWorldApp.java

If your prompt reappears without error messages, congratulations. You have successfully compiled your program.

Error Explanation

Bad command or file name (Windows 95/98)

The name specified is not recognized as an internal or external command, operable program or batch file (Windows NT)

'javac' is not recognized as an internal or external command, operable program or batch file. (Windows XP)

If you receive this error, Windows cannot find the Java compiler (javac).

Here's one way to tell Windows where to find javac. Suppose you installed the JDK in C:\jdk5.0. At the prompt you would type the following command and press Enter:

C:\jdk5.0\bin\javac HelloWorldApp.java
Note: If you choose this option, each time you compile or run a program, you'll have to precede your javac and java commands with C:\jdk5.0\bin\. To avoid this extra typing, consult the section Update the PATH variable (outside of the tutorial) in the installation instructions.

The compiler has generated a bytecode file, HelloWorldApp.class. At the prompt, type dir to see the new file that was generated, as shown in the following figure.

Directory listing, showing the generated .class file

Directory listing, showing the generated .class file

Now that you have a .class file, you can run your program.

top

Run the Program

In the same directory, enter at the prompt:
java HelloWorldApp

The next figure shows what you should now see.

The program prints

The program prints "Hello World!" to the screen.

Congratulations! Your program works.

Error Explanation

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp

If you receive this error, java cannot find your bytecode file, HelloWorldApp.class.

One of the places java tries to find your .class file is your current directory. So if your .class file is in C:\java, you should change your current directory to that. To change your directory, type the following command at the prompt and press Enter:

cd c:\java

The prompt should change to C:\java>. If you enter dir at the prompt, you should see your .java and .class files. Now enter java HelloWorldApp again.

If you still have problems, you might have to change your CLASSPATH variable. To see if this is necessary, try clobbering the classpath with the following command.

set CLASSPATH=

Now enter java HelloWorldApp again. If the program works now, you'll have to change your CLASSPATH variable. To set this variable, consult the Update the PATH variable (outside of the tutorial) section in the installation instructions. The CLASSPATH variable is set in the same manner.

top

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.