Open In App

Different Ways to Run Applet in Java

Improve
Improve
Like Article
Like
Save
Share
Report

Applets are tiny Java programs that can be installed and run automatically as part of a web page. They can be viewed on an Internet server, transmitted over the Internet, and installed and run automatically as part of a web document or desktop application.

Note:
Java applet is deprecated, which means it’s no longer recommended for use and may be removed in  future versions of the language. However, it is still supported in current versions of Java.  As applets are no longer widely used, Java developers are encouraged to use alternative technologies  such as web-based applications or JavaScript. Nevertheless, the applet technology is still available  for those who need it, but it’s not recommended for new projects. There are two standard methods for running an applet:

  1. By using Web Browser 
  2. By using Applet Viewer 

Let’s understand each technique one by one

1. Executing the applet within a Java-compatible Web browser:

 Suppose we have a GfgApplet.java file in which we have our java code. 

Java




// Java program to run the applet
// using the web browser
 
import java.awt.*;
import java.applet.*;
public class GfgApplet extends Applet
{
     String msg="";
     public void init()
    {
         msg="Hello Geeks";
     }
 
     public void start()
     {
         msg=msg+",Welcome to GeeksForGeeks";
     }
 
     public void paint(Graphics g)
     {
         g.drawString(msg,20,20);
     }
}


  • Compiling: javac GfgApplet.java
  • Create an Html file and embed the Applet tag in the HTML file.

To run an applet in a web browser, we must create an HTML text file with a tag that loads the applet. For this, we may use the APPLET or OBJECT tags. Here is the HTML file that runs HelloWorld with APPLET:

Attributes in applet tag:

  • Code: It specifies the name of the applet class to load into the browser.
  • Width: It specifies the width of an applet.
  • Height: It sets the height of an applet.

GfgApplet.html

<html>
<body>
<applet code="GfgApplet.class" width=300 height=100></applet>
</body>
</html> 

Output on browser:

The applet GfgApplet.class is loaded into the browser when you access GfgApplet.html.

To load applet programs, the browser must have java enabled.

2. Using an Applet Viewer to run the applet:

It is a java application that allows you to view applets. It’s similar to a mini-browser that will enable you to see how an applet might appear in a browser. It recognizes the APPLET tag and uses it during the creation process. The APPLET tag should be written in the source code file, with comments around it.

  • Write HTML APPLET tag in comments in the source file.
  • Compile the applet source code using javac.
  • Use applet viewer ClassName.class to view the applet.

Java




// Java program to run the applet
// using the applet viewer
 
import java.awt.*;
import java.applet.*;
public class GfgApplet extends Applet
{
     String msg="";
     public void init()
    {
         msg="Hello Geeks";
     }
 
     public void start()
     {
         msg=msg+",Welcome to GeeksForGeeks";
     }
 
     public void paint(Graphics g)
     {
         g.drawString(msg,20,20);
     }
     
}
/*
<applet code="GfgApplet" width=300 height=100>
</applet>
*/


To use the applet viewer utility to run the applet, type the following at the command prompt:

c:\>javac GfgApplet.java
c:\>appletviewer GfgApplet.java

Output:



Last Updated : 11 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads