Applets and Applications

Java programs are created in two different forms:  Applets or Applications.  Applets are small programs which run in (or are ‘hosted’ by) another application, typically a web browser or an Applet Viewer utility.  Applications run on their own (though they, like Applets, still require a JVM).  It is possible to create a Java program that is both an Applet and an Application, with some limitations.


Applets and Applications can be summarized as:


Java application

Java applet

Must be installed on local machine

Needs no explicit installation on local machine

Must be run explicitly within a Java-compatible virtual machine

Loads and runs itself automatically in a Java-enabled browser

Can run with or without a graphical user interface

Must run within a graphical user interface (using the Abstract Window Toolkit)

Starts execution with its main method

Starts execution with its init method

Once started, manages its own flow of execution

Has its flow of execution determined partly by its browser context 

Has no inherent security restrictions (apart from safety features in the Java language itself)

Has significant security controls to prevent malicious or poorly written applets from harming the user's system



Some of the examples here, and throughout the class will assume that you have your PATH and CLASSPATH environment variables setup correctly.  These settings are used both at compile and run time to build and execute your Java programs.


Setting up the PATH command for a DOS shell can done fairly easily via a batch file, or can be set in your system environment, based on your operating system.  If you installed your Jbuilder to the default folder, then you should be able to create a batch file that contains the following:


PATH = %PATH%;"C:\Program Files\JBuilder4\jdk1.3\bin"


This should be placed in a simple batch file (named something like SetJavaPath.bat), and executed once for each DOS shell. 


Your CLASSPATH can be set in a similar fashion in the same batch file with the following command (which can be added above or below the ‘PATH’ line):

SET CLASSPATH = C:\Program Files\JBuilder4\jdk1.3\demo\jfc\Java2D\Java2Demo.jar;C:\Program Files\JBuilder4\jdk1.3\jre\lib\i18n.jar;C:\Program Files\JBuilder4\jdk1.3\jre\lib\jaws.jar;C:\Program Files\JBuilder4\jdk1.3\jre\lib\rt.jar;C:\Program Files\JBuilder4\jdk1.3\jre\lib\sunrsasign.jar;C:\Program Files\JBuilder4\jdk1.3\lib\dt.jar;C:\Program Files\JBuilder4\jdk1.3\lib\tools.jar" 


The above line is an ‘all-inclusive’ line, and is just 1 line.  This means that it specifies folder not used this early in the class (in fact, you don’t need to set the CLASSPATH at all for the examples on this document).


A simple example application, can be written as:


public class CoreTools {

  public static void main( String[] args )


     System.out.println( "Hello world" );




The above file should be named  To compile the source code, use the javac tool, which is part of the Java Development Kit (JDK):  javac


This step creates a Java class file, which is run by the Java virtual machine, using the following:


java CoreTools




A simple Applet program can be written as:


import java.applet.Applet;

import java.awt.Graphics;


public class CoreToolsApplet extends Applet


       public void paint( Graphics g )


              g.drawString( "Howdy", 0, 20 );




Compiling an Applet also uses the javac compiler, as the Application did:



Again, as with the Application example, the output file will be called CoreToolsApplet.class.  In order to run this program, you need to create a simple HTML page, which will load the applet (there is also an Applet Viewer utility in the JDK).  The HTML file must include the APPLET tag, to use the new class file:





This is an Applet demo<BR>

<applet code = "CoreToolsApplet.class" width = 200 height = 200 ></Applet>