How to run Java Applet

Applets

Start a MS-DOS command window.

Now, change to the directory of your choice from within the command window.
For example:

>cd \Temp

Create some Java source code with Notepad from within the command window.
For example, suppose we want a class named “A”,
then we would start Notepad with the name of the Java file:

>notepad A.java

The source code, A.java, might look like this:

// This program draws a tall rectangle via the A.html applet tag.
// mlc
// 9/1999

import java.applet.*;
import java.awt.*;

public class A extends Applet
{
private int w, h;
public void init( )
{
w = 45;
h = 50;
}

public void paint(Graphics g)
{
g.drawRect(w, h, 20, 80);
}
}

Don’t forget to save the file!

Double check the name of the Java program file you just saved
by listing out the files in the current directory:

>dir /p

If the name of the Java program file listed does not end in .java,
you need to rename it so that it does
For instance, if the file appears as A.java.txt, type

>ren A.java.txt A.java

Now if you list the files in the directory, you should see it named correctly

Now, since this is an applet, we’ll need to make an HTML file.
We could call it A.html and create it from the command line using Notepad again:

>notepad A.html

The A.html file might look like this:

This file launches the ‘A’ applet: A.class!


No Java?!

Again, be sure to save the file
Also be sure that the name of the file ends in .html (not .txt)

Assuming we saved the Java source code (as in step 4) from within Notepad,
we can now go back to the command prompt and compile it.
This is done with the javac compiler command:

>javac A.java

If everything compiles fine
(we know this by the fact we had no error messages
and the “A.class” file exists),
we are ready to run it.
Otherwise, we have to go back to Notepad and edit the A.java
until there are no more mistakes.
Then we repeat step 7 after saving the changes.

At this point (assuming we saved the HTML file in step 5),
we can run either a Java-enabled browser or
simply use the appletviewer program from the command line:

>appletviewer A.html

The above command should bring up a window using the HTML file and
then launch (or run) the Java applet from within the viewer.

This file launches the ‘A’ applet: A.class!
No Java?!

You should see a tall rectangle. Good work!

Leave a Reply

Your email address will not be published. Required fields are marked *