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

Trail:
Getting Started



Help on searching
is available.

Search technology
by Excite, Inc.

The "Hello World" Applet

By following the steps on this page, you can create and use an applet. If you aren't interested in applets, you might want to skip ahead to the Learning the Java Language(in the Learning the Java Language trail) trail.

Create a Java Source File

Create a file named HelloWorld.java with the Java code shown here:
import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello world!", 50, 25);
    }
}

Compile the Source File

Compile the source file using the Java compiler.

If the compilation succeeds, the compiler creates a file named HelloWorld.class in the same directory (folder) as the Java source file (HelloWorld.java). This class file contains Java bytecodes.

If the compilation fails, make sure you typed in and named the program exactly as shown above. If you can't find the problem, see Common Compiler and Interpreter Problems(in the Getting Started trail).

Create an HTML File that Includes the Applet

Using a text editor, create a file named Hello.html in the same directory that contains HelloWorld.class. This HTML file should contain the following text:
<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>

Run the Applet

To run the applet, you need to load the HTML file into an application that can run Java applets. This application might be a Java-compatible browser or another Java applet viewing program, such as the Applet Viewer provided in the JDK. To load the HTML file, you usually need to tell the application the URL of the HTML file you've created. For example, you might enter something like the following into a browser's URL or Location field:
file:/home/kwalrath/HTML/Hello.html

Once you've successfully completed these steps, you should see something like this in the browser window:

What Next?

Now you can:
  • Continue on in this lesson to learn about the anatomy of applets and about importing classes.
  • Go back to The Anatomy of a Java Application(in the Getting Started trail) if you haven't already read it and you're interested in a quick introduction to key Java concepts.
  • Learn more about writing applets by going to the Writing Applets(in the Writing Applets trail) trail.
  • Go back to the Trail Map(the trail map) to get an overview of the trails you can follow.
 
Previous Page Next Page Start of Tutorial > Start of Trail Lesson Contents