An Applet is a small program that can be sent across the Internet and interpreted on a client machine. To give permission for remote access it must be a public class. Typically it is a class that inherits and/or defines a special set of functions needed to run an applet. These are part of the class Applet. So all Java applets are public classes that extend Applet.
You can not run a Java applet unless it is also an application or you have a WWW page that refers to it.
The page needs HTML like the following to call the compiled code, in the same directory:
where h and w are the HEIGHT and WIDTH of the box in which the applet outputs its response. The alternate text appears when a browser can not handle APPLETs. The code in "ClassName.class" above, is the result of compiling a file called "ClassName.java" that contains a public class called ClassName which extends an Applet.
Example
Here is a suitable piece of HTML to test a simple HelloWorld class:
Test
Put this in a file called:
test.HelloWorld.html
The code for the HelloWorld applet has to be a public class called "HelloWorld" that extends Applet and is in in a Java file called:
HelloWorld.java
Here is the Java code:
(HelloWorld):
The Applet is compiled just like any other program:
javac HelloWorld.java
This will generate a set of files with extension/suffix ".class". Notice that the compiler forces you to name the file "HelloWorld.java", the Class "HelloWorld", and generates the binary bytecode in a file called "HelloWorld.class". Sun has written a special program to test applets in page:
appletviewer test.HelloWorld.html
However you can not use 'java' to run the HelloWorld class - it has no 'main'. Neither can 'java' "run" the WWW pages like the test.HelloWorld.html file.
You can not run a Java applet unless it is also an application or you have a WWW page that refers to it.
The page needs HTML like the following to call the compiled code, in the same directory:
HTML Code:
<APPLET CODE="ClassName.class" HEIGHT=h WIDTH=w> Alternate text </APPLET>
Example
Here is a suitable piece of HTML to test a simple HelloWorld class:
Test
HTML Code:
<head> <APPLET CODE="HelloWorld.class" HEIGHT=150 WIDTH=150> You can not see this brilliant Java Applet. </APPLET> </body>
test.HelloWorld.html
The code for the HelloWorld applet has to be a public class called "HelloWorld" that extends Applet and is in in a Java file called:
HelloWorld.java
Here is the Java code:
(HelloWorld):
Code: Java
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet {
public void init() {
resize(150,25);
}//init
public void paint(Graphics g) {
g.setFont(new Font("Helvetica", Font.PLAIN, 8));
g.drawString("Hello world!", 50, 25);
}//paint
}//HelloWorld
The Applet is compiled just like any other program:
javac HelloWorld.java
This will generate a set of files with extension/suffix ".class". Notice that the compiler forces you to name the file "HelloWorld.java", the Class "HelloWorld", and generates the binary bytecode in a file called "HelloWorld.class". Sun has written a special program to test applets in page:
appletviewer test.HelloWorld.html
However you can not use 'java' to run the HelloWorld class - it has no 'main'. Neither can 'java' "run" the WWW pages like the test.HelloWorld.html file.

