Noel Enete
Minimalist Programs
NUGGET Jump Start

There are two types of Java programs: applications and applets. This nugget describes a hello world application and a hello world applet.

A Java Application

Java provides a full featured environment that will sustain a wide-range of development activity. Programs that are NOT launched from a Web browser are called Java Applications.

To write a Java application, open a new file in an editor and code the class structure in the example below. The name of the class in the source and the name of the file must be the same (including upper and lower case). The maximum length of the name is determined by the maximum length of a filename in the local file system.

Here is the famous "Hello world" program as a Java application.

    class HelloApplication
      public static void main (String args[])
        System.out.println ("Hello world!");

The name of the file for this program is If Nuggets for Java was installed into the \java\javanuggets directory, one could find this file in the directory \java\javanuggets\pages\examples\HelloApplication.

There is a single function inside the HelloApplication class. It is called main() and it accepts an array of String variables as parameters and does not return any value (void). This function is the entry point to the application. The function contains a single call to the println() function to print the string "Hello world!".

To compile this program, switch to its directory and invoke the compiler followed by the file name of the source file.


Every Java class compiles down to a .class file. The output of this compile is the executable HelloApplication.class file that contains instructions for the Java Virtual Machine.

To run this program, invoke Java's Virtual Machine and pass it the name of the class. It will print the message Hello world!.

    java HelloApplication
    Hello world!

The javac.exe and javac.exe programs are located in Java's binary directory (\java\java.117\bin). The java.exe program invokes Virtual Machine routines to load and run the HelloApplication.class. As the class runs, the main() function is called and the "Hello world!" string is sent to the standard output device.

To construct a general purpose Java application a person simply needs to construct a class with a main() function. This kind of program runs independent of a Web browser and can generate complete GUI windows, perform file I/O, and access Internet resources.

A Java Applet

If that is how to construct a regular Java program, how does one construct the special kind of Java program that is launched from a Web page? -- the steps are very similar.

One opens a new file and codes a class structure in it. The name of the file and the name of the class must be the same. But now, the class statement must also say that it extends Applet.

import java.applet.Applet;
import java.awt.Graphics;

    public class HelloApplet extends Applet
      public void paint (Graphics g)
        g.drawString ("Hello World!", 50, 25);

"Applet" is the name of the Java class that can ride in a Web page. When a class extends the Applet class, the new class becomes an extension of the Web riding class and so can be launched by a Web page. Since this kind of Java program works so closely with the Applet class, the Java program is called a Java applet.

The second difference is that an applet has a paint() function rather than a main() function. The paint() function gets called each time the applet's region on the Web page needs to be refreshed. A Graphics object is passed to the paint() function and nothing (void) is returned.

The Graphics object is responsible for marking on the display surface. In the example above, the Graphics object is given the name g. Then the drawString() function is called to print the string "Hello World!" on the display surface.

The parameters 50 and 25 identify the (x, y) position at which to place the string. This is precisely the lower left corner of the baseline upon which the string characters are drawn as measured in pixels from the upper left corner.

Notice that there are two new classes referenced in this program -- the Applet class and the Graphics class. Whenever, a class outside of the current source file is referenced, an import statement must be coded to tell the compiler where to find the external class.

Related classes are stored together in a unit called a package. The Applet class is part of the java.applet package and the Graphics class is part of the java.awt package. By coding the package name with the class name in the import statement, the compiler has all the information that is necessary to include the class in the compilation.

The last way an applet differs from an application is that it must be launched from a Web page rather than from a command line. The special applet tag must be added to a Web page to identify the applet to launch. Here is the file HelloApplet.html Web page that launches the HelloApplet class above.


<TITLE> HelloApplet </TITLE>


<APPLET CODE="HelloApplet" WIDTH=150 HEIGHT=50>


The <APPLET CODE= ____ > . . . </APPLET> tag identifies the Java applet to launch. When the browser loads the Web page, it sees that the page requires an applet and submits a separate request to the server to fetch the HelloApplet.class. The browser knows the directory from which it loaded the Web page. By default, it looks to that same directory to find the Java class files. When the class is received, the browser loads it and runs it.

The phrase WIDTH=150 HEIGHT=50 in the APPLET tag defines the size of the region on the Web page that the applet owns. The browser places that rectangle on the Web page in the same way that it places a rectangular image on the Web page. It is treated as a tall word in a flow of HTML text.

Although it is always possible to display an applet in a Web browser, during development, it is desirable to test the applet with the appletviewer provided by Java. The appletviewer.exe is a program in Java's bin directory that interprets the <APPLET> tag in a Web page and runs an applet in a simulated Web browser window. It is more forgiving than a full Web browser and provides a friendlier test environment.

The following sequence compiles the HelloApplet and launches the appletviewer to run it.

Figure 1: HelloApplet


C:\java\javanuggets\pages\examples\HelloApplet>appletviewer HelloApplet.html


The Java programming language can be used to write programs that execute apart from a Web browser. These programs are called Java applications and can serve a wide variety of programming needs.

The special kind of Java program that is attached to a Web page is called a Java applet. It is able to be communicated across the Internet in the same way that a Web page is communicated. When a Web page and applet arrive at a Web browser, the applet is launched by the browser and begins drawing on a section of the Web page.

by Noel Enete . . . . . .