Building Your First Java Applet

Screeshot of a Java web page.

fsse8info / Flickr / CC BY 2.0

 Before you start this tutorial, you must have downloaded and installed the Java SE Development Kit.

Java applets are like Java applications, their creation follows the same three-step process of write, compile and run. The difference is, instead of running on your desktop, they run as part of a web page.

The goal of this tutorial is to create a simple Java applet. This can be achieved by following these basic steps:

  1. Write a simple applet in Java
  2. Compile the Java source code
  3. Create an HTML page that references the applet
  4. Open the HTML page in a browser
01
of 09

Write the Java Source Code

Writing the Source Code
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

This example uses Notepad to create the Java source code file. Open up your chosen editor, and type in this code:

Don’t worry too much about what the code means. For your first applet, it’s more important to see how it’s created, compiled and run.

02
of 09

Save the File

Save the File
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Save your program file as "FirstApplet.java". Make sure the filename you use is correct. If you look at the code you will see the statement:

It’s an instruction to call the applet class "FirstApplet". The filename must match this class name, and have an extension of ".java". If your file is not saved as "FirstApplet.java", the Java compiler will complain and not compile your applet.

03
of 09

Open a Terminal Window

Run Dialog Box
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

To open a terminal window, press the “"Windows key" and the letter "R".

You will now see the "Run Dialog". Type "cmd", and press "OK".

A terminal window will appear. Think of it as a text version of Windows Explorer; it will let you navigate to different directories on your computer, look at the files that they contain, and run any programs that you want to. This is all done by typing commands into the window.

04
of 09

The Java Compiler

Compile the Applet
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

We need the terminal window to access the Java compiler called "javac". This is the program that will read the code in the FirstApplet.java file, and translate it into a language that your computer can understand. This process is called compiling. Just like Java applications, Java applets must be compiled too.

To run javac from the terminal window, you need to tell your computer where it is. On some machines, it’s in a directory called "C:\Program Files\Java\jdk1.6.0_06\bin". If you don’t have this directory, then do a file search in Windows Explorer for "javac" and find out where it lives.

Once you’ve found its location, type the following command into the terminal window:

E.g.,

Press Enter. The terminal window won’t do anything flashy, it will just return to the command prompt. However, the path to the compiler has now been set.

05
of 09

Change the Directory

Change Directory
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Navigate to where the FirstApplet.java file is saved. For example: "C:\Documents and Settings\Paul\My Documents\Java\Applets".

To change the directory in the terminal window, type in the command:

E.g.,

You can tell if you’re in the right directory by looking to the left of the cursor.

06
of 09

Compile the Applet

Compile the Applet
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

We’re now ready to compile the applet. To do so, enter the command:

After you hit Enter, the compiler will look at the code contained within the FirstApplet.java file, and attempt to compile it. If it can’t, it will display a series of errors to help you fix the code.

The applet has been compiled successfully if you are returned to the command prompt without any messages. If that’s not the case, go back and check the code you’ve written. Make sure it matches the example code and re-save the file. Keep doing this until you can run javac without getting any errors.

Tip: Once the applet has been successfully compiled, you will see a new file in the same directory. It will be called “FirstApplet.class”. This is the compiled version of your applet.

07
of 09

Create the HTML File

HTML File
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

It’s worth noting that so far you have followed exactly the same steps as you would if you were creating a Java application. The applet has been created and saved in a text file, and it has been compiled by the javac compiler.

Java Applets differ from Java applications when it comes to running them. What’s needed now is a web page that references the FirstApplet.class file. Remember, the class file is the compiled version of your applet; this is the file your computer can understand and execute.

Open up Notepad, and type in the following HTML code:

Save the file as “MyWebpage.html” in the same directory as your Java applet files.

This is the most important line in the webpage:

When the web page is displayed, it tells the browser to open up your Java applet and run it.

08
of 09

Open the HTML Page

A Simple Java Applet
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

The last step is the best one; you get to see the Java applet in action. Use Windows Explorer to navigate to the directory where the HTML page is stored. For example, "C:\Documents and Settings\Paul\My Documents\Java\Applets" with the other Java applet files.

Double-click on the MyWebpage.html file. Your default browser will open, and the Java applet will run.

Congratulations, you have created your first Java applet!

09
of 09

A Quick Recap

Take a moment to review the steps you took to create the Java applet. They will be the same for every applet you make:

  1. Write the Java code in a text file
  2. Save the file
  3. Compile the code
  4. Fix any errors
  5. Reference the applet in a HTML page
  6. Run the applet by viewing the web page
Format
mla apa chicago
Your Citation
Leahy, Paul. "Building Your First Java Applet." ThoughtCo, Aug. 28, 2020, thoughtco.com/building-your-first-java-applet-2034332. Leahy, Paul. (2020, August 28). Building Your First Java Applet. Retrieved from https://www.thoughtco.com/building-your-first-java-applet-2034332 Leahy, Paul. "Building Your First Java Applet." ThoughtCo. https://www.thoughtco.com/building-your-first-java-applet-2034332 (accessed March 28, 2024).