Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Detailed Instructions
for Your First ProgramThe following instructions will help you write your first program. These instructions are for users of Win32 platforms, which include Windows 95/98 and Windows NT/2000. We start with a checklist of what you need to write your first program. Next, we cover the steps to creating an application, steps to creating an applet, and explanations of error messages you may encounter.
1. A Checklist 2. Creating Your First Application
a. Create a Java Source File
b. Compile the Source File
c. Run the Program3. Creating Your First Applet 4. 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.
To write your first program, you need:
1. A ChecklistThese two items are all you need to write your first Java program. You may want to consider using an IDE to help you write your programs. Java 2 SDK, Standard Edition v. 1.3, is available bundled with an IDE, the ForteTM for JavaTM, Community Edition.
- The JavaTM 2 Platform, Standard Edition. You can download it now and consult the installation instructions.
- A text editor. In this example, we'll use NotePad, the simple editor included with the Windows platforms. To find NotePad, from the Start menu select Programs > Accessories > NotePad. You can easily adapt these instructions if you use a different text editor.
a. Create a Source File.
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 step b.
HelloWorldApp.java
- Or, you can follow these longer instructions:
1. Start NotePad. In a new document, type in the following code:
/** * The HelloWorldApp class implements an application that * displays "Hello World!" to the standard output. */ public class HelloWorldApp { public static void main(String[] args) { // Display "Hello World!" System.out.println("Hello World!"); } }2. Save this code to a file. From the menu bar, select File > Save As. In the Save As dialog box:
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
- Using the Save in drop-down menu, specify the folder (directory) where you'll save your file. In this example, the directory is
java
on theC
drive.- In the File name text box, type
"HelloWorldApp.java"
, including the double quotation marks.- From the Save as type drop-down menu, choose Text Document.
When you're finished, the dialog box should look like this:topNow click Save, and exit NotePad.
b. Compile the Source File.
topFrom 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 yourC
drive (as shown above) orWINNT
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 isjava
on theC
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 theD
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
(Windows 95/98)
The name specified is not recognized as an internal or external command, operable program or batch file
(Windows NT)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 inC:\jdk1.3
. 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:\jdk1.3\bin\javac HelloWorldApp.java
javac
andjava
commands withC:\jdk1.3\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, typedir
to see the new file that was generated:Now that you have a .class
file, you can run your program.
c. Run the Program.
In the same directory, enter at the prompt:Now you should see:
java HelloWorldApp
Congratulations! Your program works. top
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 inC:\java
, you should change your current directory to that. To change your directory, type the following command at the prompt and press Enter:The prompt should change tocd c:\java
C:\java>
. If you enterdir
at the prompt, you should see your.java
and.class
files. Now enterjava 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:
Now enterset CLASSPATH=
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.
3. Creating Your First Applet
HelloWorldApp
is an example of a Java application, a standalone program. Now you will create a Java applet calledHelloWorld
, which also displays the greeting "Hello world!". UnlikeHelloWorldApp
, however, the applet runs in a Java-enabled Web browser such as HotJava, Netscape Navigator, or Microsoft Internet Explorer.To create this applet, you'll perform the basic steps as before: create a Java source file; compile the source file; and run the program.
a. Create a Java Source File.
Again, you have two options:
- You can save the files
HelloWorld.java
andHello.html
on your computer and avoid a lot of typing. Then, you can go straight to step b.- Or, you can follow these instructions:
1. Start NotePad. Type the following code into a new document:
import java.applet.*; import java.awt.*; /** * The HelloWorld class implements an applet that * simply displays "Hello World!". */ public class HelloWorld extends Applet { public void paint(Graphics g) { // Display "Hello World!" g.drawString("Hello world!", 50, 25); } }
Save this code to a file called
HelloWorld.java
.2. You also need an HTML file to accompany your applet. Type the following code into a new NotePad document:
<HTML> <HEAD> <TITLE>A Simple Program</TITLE> </HEAD> <BODY> Here is the output of my program: <APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25> </APPLET> </BODY> </HTML>
Save this code to a file called
Hello.html
.
b. Compile the Source File.
At the prompt, type the following command and press Return:
javac HelloWorld.java
The compiler should generate a Java bytecode file,
HelloWorld.class
.
c. Run the Program.
topAlthough you can view your applets using a Web browser, you may find it easier to test your applets using the simple
appletviewer
application that comes with the JavaTM Platform. To view theHelloWorld
applet usingappletviewer
, enter at the prompt:
appletviewer Hello.html
Now you should see:
Congratulations! Your applet works.
4. Where to Go from Here
To continue your introduction to the Java programming language, check out these trails:
Getting Started Learning the Java Language
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.