Exploring the Basics of Applet in Java: A Beginner’s Guide
By Sriram
Updated on Jul 09, 2025 | 9 min read | 6.25K+ views
Share:
For working professionals
For fresh graduates
More
By Sriram
Updated on Jul 09, 2025 | 9 min read | 6.25K+ views
Share:
Table of Contents
Did you know? As of 2025, Java applets have officially been phased out! After being deprecated in JDK 9 (2017) and slated for removal in JDK 17 (2021), the Applet API is no longer part of the Java platform. This shift was driven by the end of browser support for NPAPI plugins and the removal of the appletviewer tool in JDK 11 (2018). |
Applets in Java are small, platform-independent programs that run within web browsers, providing dynamic content and interactivity without requiring installation. These applets were designed to allow developers to create rich web applications, such as real-time data visualization or interactive educational tools, all within a browser environment.
In this blog, you’ll learn about the applet in Java, covering their key features, life cycle, usage, and how to run them using HTML files and Appletviewer.
Strengthen your understanding of Java applets with upGrad’s Software Development courses! Learn everything from basic Java to advanced frameworks, preparing you for the future of tech. Join now and explore endless opportunities.
Java applets are lightweight, platform-independent programs that run directly within web browsers. They provide dynamic content through graphical rendering and user interaction, without the need for installation. Additionally, built-in security features ensure safe execution within the browser.
In 2025, professionals who can use advanced Java programming techniques like applets to streamline business operations will be in high demand. If you're looking to develop skills in programming, here are some top-rated courses to help you get there:
Note: While Java applets are now deprecated and no longer supported in most browsers, they remain important for understanding legacy systems and the evolution of Java development.
Now that we understand the basics of Java applets, let's explore their key features that make them unique and valuable in programming.
Key Features of Applet in Java
Applet in Java offers several distinct features that make them suitable for interactive applications on the web. These features allow applets to run within web browsers, interact with users, and display dynamic content.
Let’s explore these key features in more detail.
Despite their initial success, the popularity of applet in Java began to fade as new web technologies emerged. HTML5, JavaScript, and other technologies quickly overtook Java applets in providing interactive web content.
For example, you can now achieve animation effects and dynamic content using JavaScript, without the need for a plugin or a separate runtime environment.
Where Are Java Applets Used Today?
Even though the applet in Java has been largely replaced by other technologies, it’s not entirely gone. There are still areas where they are useful:
You can get a better hang of Java with upGrad’s free Core Java Basics course. It covers variables, data types, loops, and OOP principles to build strong coding skills. Perfect for aspiring developers, students, and professionals transitioning to Java.
Also Read: Difference Between Applet and Application in Java: Features, Benefits, and Uses
Having explored the basics of applets in Java, it's time to take a closer look at their life cycle and the various phases they undergo.
Applet in Java follows a specific life cycle that governs how it is created, executed, and terminated. Its lifecycle is managed by the browser or the environment hosting it.
The life cycle of an applet is made up of several method calls, each of which serves a unique purpose in initializing, running, and shutting down the applet. These methods ensure that the applet runs smoothly and allows developers to manage resources effectively.
Here’s a look at the main phases that happen when an applet in Java starts and stops:
The sequence of these method calls ensures that your applet in Java is properly managed throughout its lifecycle.
To dive deeper, let’s explore each of the key methods involved in the applet in Java life cycle:
destroy() Method: The destroy() method is called when the applet is permanently removed from memory, usually when the browser window is closed or the applet is destroyed by the browser environment. It is where you clean up resources, such as closing database connections or stopping threads.
Why they matter: These methods ensure that the applet does not waste resources once it’s no longer needed and that it’s gracefully terminated without leaving any open processes.
Also Read: Java Servlets by Example: Master Lifecycle & Best Practices
Having understood the core life cycle methods, let’s explore a practical Java Applet example to see how these methods are used in action.
upGrad’s Exclusive Software Development Webinar for you –
SAAS Business – What is So Different?
The example below shows the sequence of method calls when the applet starts, runs, and terminates.
import java.applet.Applet;
import java.awt.Graphics;
public class AppletLifeCycleExample extends Applet {
// Initialization method
public void init() {
System.out.println("Applet Initialized");
}
// Start method
public void start() {
System.out.println("Applet Started");
}
// Paint method to render graphics
public void paint(Graphics g) {
g.drawString("Hello, Applet!", 50, 50);
System.out.println("Applet Painted");
}
// Stop method
public void stop() {
System.out.println("Applet Stopped");
}
// Destroy method
public void destroy() {
System.out.println("Applet Destroyed");
}
}
Output:
Applet Initialized
Applet Started
Applet Painted
Applet Stopped
Applet Destroyed
In this simple example, each method corresponds to a specific stage in the applet in the Java life cycle. First, the init() method is called to initialize the applet, followed by start(), which begins the applet’s execution. The paint() method handles the graphical rendering of content, and eventually, when the applet is stopped and destroyed, the stop() and destroy() methods are invoked.
With the life cycle of a Java applet clear, let's discuss the process of running your first applet in Java and seeing it in action.
If you’ve written an applet in Java and are ready to see it in action, you need to know how to run it properly. In the past, Java applets were executed within browsers or with tools like Appletviewer. But as browsers moved away from supporting Java applets, it's important to understand how to run them in both environments.
Here’s how you can get your applet in Java running, step by step.
One of the most common ways to run an applet in Java is by embedding it within an HTML file. This method makes your applet accessible through a browser that supports Java plugins, although it’s less common nowadays due to security concerns and browser updates.
To embed an applet in Java in an HTML file, follow these steps:
1. Write the Applet Code
First, ensure your Java applet code is ready. Here's a simple example of an applet in Java that displays a message.
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello, Applet in Java!", 50, 50);
}
}
2. Compile the Applet
Next, compile the Java code. Open your terminal (or Command Prompt) and run the following command:
javac HelloWorldApplet.java
This will create a file called HelloWorldApplet.class, which is your compiled applet.
3. Create an HTML File
After compiling the applet, create an HTML file to embed the applet. Here's the HTML code you’ll use:
<html>
<head>
<title>Java Applet Example</title>
</head>
<body>
<h2>Java Applet Example</h2>
<applet code="HelloWorldApplet.class" width="300" height="300">
</applet>
</body>
</html>
4. Open the HTML File in a Browser
Once your HTML file is ready, open it in a browser that supports Java applets (such as older versions of Firefox or Internet Explorer). You should see the applet running in the browser with the message “Hello, Applet in Java!” displayed.
Even though most modern browsers no longer support Java applets, this method was a common way to run applets in Java back in the day. For now, this method remains useful for learning about applets and legacy systems.
The Appletviewer tool is a command-line utility provided by the Java Development Kit (JDK). It was designed specifically to run applets in Java outside the browser. This is especially helpful for testing and debugging applets without worrying about browser support. Here's how you can use the Appletviewer tool to run an applet in Java:
1. Ensure JDK is Installed
To use Appletviewer, you must have the Java Development Kit (JDK) installed. If you haven't installed it yet, download the JDK from Oracle's website and follow the installation instructions for your system.
2. Write Your Applet Code
Here’s a simple example of an applet in Java:
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello, Applet in Java!", 50, 50);
}
}
3. Compile the Applet
Use the terminal or command prompt to compile your applet in Java:
javac HelloWorldApplet.java
4. Create an Applet HTML File
Even when using Appletviewer, you still need to create an HTML file that tells the tool how to locate and run your applet. Here’s the corresponding HTML file:
<html>
<head>
<title>AppletViewer Example</title>
</head>
<body>
<h2>Running Applet in Java</h2>
<applet code="HelloWorldApplet.class" width="300" height="300">
</applet>
</body>
</html>
5. Run the Applet Using Appletviewer
Now that you have both the .class file and the HTML file, use the Appletviewer tool to run the applet:
appletviewer HelloWorldApplet.html
This command will launch the Appletviewer tool and display the applet in a window.
6. View the Output
Once you run the above command, you will see your applet in Java rendered by the Appletviewer. The output will display “Hello, Applet in Java!” on the window, just like it would in a browser (before Java applets were deprecated).
Still writing messy or repetitive Java code? That’s a sign you need stronger OOP fundamentals. upGrad’s Java Object-Oriented Programming course helps you master classes, inheritance, and encapsulation, enabling you to write cleaner, modular code.
Also Read: How to Code, Compile, and Run Java Projects in 2025
Now that you know how to run an applet in Java, let's explore the key packages essential for creating and managing Java applets effectively.
Applet in Java relies on a few core packages that make the development process simpler and more efficient. These packages are built into the Java Development Kit (JDK) and offer a range of essential tools that handle everything from drawing graphics to creating interactive components.
Below are the essential packages you'll work with when creating applet in Java:
1. java.applet.Applet: Base Class for Applets
The java.applet.Applet class is the foundation of every applet in Java. It provides the essential methods and structure needed to create applets. All applets must extend this class in order to be recognized as applets by the Java Runtime Environment (JRE).
2. java.awt.Graphics: Essential for Rendering Graphics in Applets
Once you’ve extended the Applet class, you’ll want to display something to the user. This is where the java.awt.Graphics package comes into play. It provides the classes needed to render 2D animation/graphics on the applet window.
3. java.awt: GUI Components and Event-Handling
The java.awt package is a comprehensive package that includes the classes needed for user interface components (GUI) and event-handling. It provides everything from buttons and text fields to more complex layout managers. You’ll often use an applet in Java with components from this package to interact with users.
When developing an applet in Java, these packages work together to provide a functional and interactive user experience. The Applet class gives you the basic applet structure, Graphics lets you render visuals, and java.awt helps you create interactive GUI components. Combining these packages allows you to create powerful, engaging applets.
Also Read: 50 Java Projects With Source Code in 2025
Having learned about the key Java applet packages, you're ready to take the next step. Join upGrad and become an expert in Java applets and beyond!
Understanding the applet in Java gives you a solid foundation in how interactive web applications were originally built. To deepen your knowledge, try building simple applets and experiment with different Java packages, like java.awt.Graphics and event handling. This hands-on approach will reinforce the concepts and improve your practical skills.
In addition to the specialized courses mentioned above, here are some additional free courses to help you get started.
Still struggling to find the right Java software development course to match your goals in 2025? Reach out to upGrad for personalized counseling and valuable insights, or visit your nearest upGrad offline center for more details.
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
References:
https://www.infoworld.com/article/3623579/java-applet-api-heads-for-the-exit.html
https://incusdata.com/blog/life-and-death-of-java-applets
https://openjdk.org/jeps/504
183 articles published
Meet Sriram, an SEO executive and blog content marketing whiz. He has a knack for crafting compelling content that not only engages readers but also boosts website traffic and conversions. When he'sno...
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources