Previous | Next | Trail Map | Getting Started | Table of Contents


The "Hello World" Applet

By following the steps on this page, you can create and use an applet. When you've followed all the steps, your directory (folder) structure should look something like this:

Create a directory

Create a directory to hold your HTML pages, if you don't already have one.

Important: Do NOT invoke hotjava [or any other applet viewer?] from the HTML directory if you might want to reload the applet. Because of the way the class loader works, an applet can't be reloaded (for example, after you make changes to its code) when you invoke the applet viewer from the directory that contains the applet's compiled code.

Create a Java source file

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

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

Compile the source file

Compile the file using the Java compiler.

If compilation succeeds, the compiler creates a file named HelloWorld.class. If compilation fails, make sure you typed in and named the program exactly as shown above.

Create an HTML file that includes the applet

Create a file named Hello.html in your HTML directory containing 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>

Load the HTML file

Load the new HTML file into HotJava by entering its URL in the Document URL field near the top of the HotJava window. For example:
file:/home/kwalrath/HTML/Hello.html
Once you've successfully completed these steps, you should see the following in the HotJava page that comes up:
Here is the output of my program:

You can't run 1.0 applets. Here's what you'd see if you could:


Now what?

Now you can:


Previous | Next | Trail Map | Getting Started | Table of Contents