How to run an applet in Java?

There are two methods to run an applet in Java.

Method-1: To run it in a web browser: You can use a Java compatible web browser to run an applet. To run it in a browser, write a HTML file like:

<applet code=”TestApplet” width=300 height=100>


Where, TestAppelt is the class  name of the applet, you wish to run.  Width and height is the display area for the applet. Now, simply execute this HTML file to run the applet.

Method-2: By using Applet Viewer:

a) Create a HTML file as described above. Name it as test.html. Now enter the following in the command line:

C:\>appletviewer test.html

b) Also, you can put a comment in your Java file to run the applet. For Example:

import java.awt.*;

import java.applet.*;


<applet code=”TestApplet” width=300 height=100>



public class TestApplet extends Applet {

public void paint(Graphics g) {

g.drawString(“Test Applet”, 40, 30);



Leave a Reply