The JavaTM Tutorial

| Start of Tutorial > Start of Trail | Search Feedback Form

Trail: Getting Started

## Lesson: 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 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 Solving Common Compiler and Interpreter Problems.

### 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 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 trail.
> - Go back to the Trail Map to get an overview of the trails you can follow.

| Start of Tutorial > Start of Trail | Search Feedback Form