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

Trail: Getting Started

Lesson: Your First Cup of Java

Detailed Instructions
for Your First Program

The following instructions will help you write your first Java program. These instructions are for users of Win32 platforms, which include Windows 98, Windows 95, and Windows NT.

1. A Checklist 2. Creating Your First Program
     a. Create a Java Source File
     b. Compile the Source File
     c. Run the Program
3. Where to Go from Here

Your feedback is important to us! If you have comments about these instructions, send them to: tutorial@java.sun.com. In your message, put cup of java in the subject header.



1. A Checklist 

To write your first program, you will need:
  1. The JavaTM 2 Platform, Standard Edition. You can download it now and consult the installation instructions
  2. A text editor. In this example, we'll use WordPad, the simple editor included with the Windows platforms. To find WordPad, from the Start menu select Programs > Accessories > WordPad. You can easily adapt these instructions if you use a different text editor. 
These two items are all you need to write your first Java program.


2. Creating Your First Program

 
Why Bytecodes are Cool

So, you've heard that with the Java programming language, you can "write once, run anywhere." This means that when you compile your program, you don't generate instructions for one specific platform. Instead, you generate Java bytecodes, which are instructions for the Java Virtual Machine (Java VM). If your platform--whether it's Windows, Solaris, MacOS, or Internet browser--has the Java VM, it can understand those bytecodes.

 
Your first program, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will: 
  • Create a Java 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 bytecode file. The Java compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler puts these instructions into a bytecode file.

  •  
  • Run the program contained in the bytecode file. The Java VM is implemented by a Java interpreter, java. This interpreter takes your bytecode file and translates its instructions into instructions that your computer can understand. 

top


a. Create a Java Source File.

You have two options. If you save the file HelloWorldApp.java on your computer, you can skip to step b. Otherwise, follow these instructions:

1. Start WordPad. 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) {
        System.out.println("Hello World!"); // Display "Hello World!"
    }
}

 
 Be Careful When You Type
Type all code, commands, and file names exactly as shown. The Java compiler and interpreter are case-sensitive, so you must capitalize consistently.
 
 

HelloWorldApp  helloworldapp

2. Save this code to a file. From the menu bar, select File > Save As. In the Save As dialog box: 
    • Using the Save in drop-down menu, specify the directory (folder) where you'll save your file. In this example, the directory is java on the C drive. 
    • In the File name text box, type HelloWorldApp.java
    • From the Save as type drop-down menu, choose Text Document
When you're finished, the dialog box should look like this:

Now click Save, and exit WordPad.

3. Using Windows Explorer, go to the java directory on your C drive. From the menu bar, select View and then Folder Options (Windows 98) or Options (Windows 95/NT). In the dialog box that appears, click on the View tab. If you see a checkmark beside Hide file extensions for known file types (Windows 98/NT) or Hide MS-DOS file extensions for the file types that are registered (Windows 95), uncheck it and click OK. Your Explorer window should look like this:

If your file is named HelloWorldApp.java.txt, rename it to HelloWorldApp.java.

Note: Windows may warn you that changing the filename extension may render it unusable. Go ahead and change it anyway.

top

b. Compile the Source File.

From the Start menu, select the MS-DOS Prompt application (Windows 95/98) or Command Prompt application (Windows NT). When the application launches, it should look like this:

The prompt shows your current directory. When you bring up the prompt for Windows 95/98, your current directory is usually WINDOWS on your C drive (as shown above) or WINNT for Windows NT. To compile your source code 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, you would 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.
 
As shown here, to change to the java directory on the D drive, you must reenter the drive, d:

If you enter dir at the prompt, you should see your file.

Now you can 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

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 Java 2 Software Development Kit in C:\jdk1.2.1. At the prompt you would type the following command and press Enter

C:\jdk1.2.1\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:\jdk1.2.1\bin\. To avoid this extra typing, consult the section Update the PATH variable in the installation instructions.


The compiler has generated a Java bytecode file, HelloWorldApp.class. At the prompt, type dir to see the new file that was generated:

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

c. Run the Program.

In the same directory, enter at the prompt:
 
 java HelloWorldApp
Now you should see:

Congratulations! Your program works.

HelloWorldApp is a Java application, a standalone program. Another type of Java program is an applet--a program that can run in a Java-enabled Web browser such as HotJava, Netscape Navigator, or Microsoft Internet Explorer. For an introduction to applets, check out The "Hello World" Applet lesson in the Getting Started trail.

 
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 bytecode file is your current directory. So, if your bytecode 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. For more information, consult the section Check the CLASSPATH Variable in the installation instructions.
top

3. Where to Go from Here

To continue your introduction to the Java programming language, check out these trails:
  Getting Started
  Learning the Java Language

top


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