|      | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
 
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
and "Hello World" for Mac OS X
.

To write your first program, you need:
- The J2SE TM Development Kit 5.0 (JDK TM 5.0). You can download the Windows version now
. (Make sure you download the JDK, not the JRE.) Consult the installation instructions
.
- 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.
Your first application,
HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will:
- Create a source file. A source file contains text, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files.
- Compile the source file into a .class file. The Java compiler,
javac, takes your source file and translates its text into instructions that the Java Virtual Machine can understand. The instructions contained within this file are known as bytecodes.
- Run the program. The Java launcher (
java) uses the Java Virtual Machine to run your application.
To create a source file, you have two options:
- You can save the file
on your computer and avoid a lot of typing. Then, you can go straight to Compile the Source File.HelloWorldApp.java
- Or, you can use the following (longer) instructions.
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.
HelloWorldApphelloworldapp
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:When you're finished, the dialog box should look like this.
- Using the Save in combo box, specify the folder (directory) where you'll save your file. In this example, the directory is
javaon theCdrive.- In the File name text field, type
"HelloWorldApp.java", including the quotation marks.- From the Save as type combo box, choose Text Documents (*.txt).
- In the Encoding combo box, leave the encoding as ANSI.
The Save As dialog just before you click Save.
Now click Save, and exit NotePad.
top
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 shell window.
The prompt shows your current directory. When you bring up the prompt, your current directory is usually
C:\WINDOWSfor Windows 98,C:\WINNTfor 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
javaon theCdrive, type the following command at the prompt and press Enter:cd C:\javaNow 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 thejavadirectory on theDdrive, you must enterD:, as shown in the following figure.
Changing directory on an alternate drive.
If you enter
dirat the prompt, you should see your source file, as the following figure shows.
Directory listing, showing the
.javasource file.Now you are ready to compile. At the prompt, type the following command and press Enter.
javac HelloWorldApp.javaIf 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 inC:\jdk5.0. At the prompt you would type the following command and press Enter:Note: If you choose this option, each time you compile or run a program, you'll have to precede yourC:\jdk5.0\bin\javac HelloWorldApp.javajavacandjavacommands withC:\jdk5.0\bin\. To avoid this extra typing, consult the section Update the PATH variablein the installation instructions.
The compiler has generated a bytecode file,
HelloWorldApp.class. At the prompt, typedirto see the new file that was generated, as shown in the following figure.
Directory listing, showing the generated
.classfileNow that you have a
.classfile, you can run your program.top
In the same directory, enter at the prompt:java HelloWorldAppThe next figure shows what you should now see.
The program prints "Hello World!" to the screen.
Congratulations! Your program works.
Error Explanation 
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldAppIf you receive this error,
javacannot find your bytecode file,HelloWorldApp.class.One of the places
javatries to find your.classfile is your current directory. So if your.classfile is inC:\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:\javaThe prompt should change to
C:\java>. If you enterdirat the prompt, you should see your.javaand.classfiles. Now enterjava HelloWorldAppagain.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 HelloWorldAppagain. If the program works now, you'll have to change your CLASSPATH variable. To set this variable, consult the Update the PATH variablesection in the installation instructions. The CLASSPATH variable is set in the same manner.
top
 
|      | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.