How to Edit, Compile, and Run Java Application and Applets for COS 111

Windows 95 Version


Editing Java Programs

As with HTML, all you really need to write and edit Java code is a text editor, such as the "NotePad" editor that we have used to this point. However, for various reasons we suggest that you actually use a slightly more powerful text editor, called "TextPad". It is available from the COS 111 Applications folder on the k: drive. The basic functions of TextPad work very similarly to those in NotePad, so you should have little trouble adapting.

Java has certain requirements about how you must save files which contain Java code if you are going to make use of them. In particular, each file contains the definition for one class, and each file must be saved under the name, where classname is the name of the class which that file defines.

Note: Because of a problem with the file-serving software, you should not save your .java files directly to your space on the K: drive. The reason for this is that no matter what name you use when you say "Save As" from TextPad, the filename will come out all in lower-case letters on the K: drive. However, the Java compiler is very picky about file names, and insists that the capitalization in the file name must match the capitalization of the class declaration. Thus, it is recommended that you save all your .java files somewhere on the machine's hard drive (i.e., the C: drive) while working with them. When you are finished using them, and no longer need to worry about editing them any more, be sure to copy them over onto the K: drive, so that they will be saved in a safe place.

Compiling your Java Programs

Before you can do anything with your Java code, you have to compile it; "compiling" refers to the process by which high-level, human-readable Java code is turned into a long string of bytes (much like a machine language) which can be understood by a Java interpreter.

After you have written your code and are ready to compile it into the "byte code", you should save your work from TextPad. Be sure to follow Java file-naming conventions and save your work onto the machine's hard drive, as described above. Then, from within TextPad, choose the "Compile Java" command from the Tools menu. This will automatically invoke the Java compiler program and run it on the file you have open in TextPad (presumably, the program you just saved). Any messages that the compiler has for you (such as error messages if you typed something that's not legal in Java) will appear in your TextPad window. If everything works, a message saying "Process Completed Successfully" will appear in the lower-left corner of the window.

When the compiler is done running and you've read any messages it has for you, you can close the "compiler output" window and go back to your Java code by clicking on the lower one of the two "Close Boxes" (squares marked with an X) in the upper-right corner of the window.

If your program compiled successfully, it should have created a new file in the same place that you had saved your original source code file; this new file will have the same name as the old one, except that it will end in ".class" rather than ".java". This file contains the byte code version of the Java code you wrote.

Running Java Programs

There are two different kinds of Java programs. As is discussed in the lab, Java is basically a general-purpose programming language which will let you build any kind of program you want. On the other hand, one particular kind of program, "Java applets", are the primary reason it exists. The way in which you run applets differs greatly from the way in which you run other kinds of Java programs. You can tell what kind of Java program you have by looking at the source code. If it is an applet, then one of the classes will be declared as something like

public class classname extends Applet {
If your program is not an applet, you should find that one of your classes has a method called "main".

For Java programs which are not applets (programs which have a method called "main", usually called "Standalone" Java programs), the instructions are relatively simple. The Java interpreter does not run "run under Windows" - that is, by default it supports only the very simple kind of input and output available under older versions of Microsoft's operating system, called MS-DOS. So, to run it, we first need to run a "DOS shell", which is a simulation of the command line interface which MS-DOS provided.

First, move the .class file(s) containg your Java program onto the K: drive. Then, run a DOS shell, by selecting the "MS-DOS Prompt" option from the Start menu. When the window containing the shell pops up, type

k:\\"COS 111 Applications"\java\bin\java classname
and press return. classname is the name of the class which represents the program you which to run, e.g. "HelloWorld". If your program involves more than one class, classname should be the name of the program which contains a mathod named "main".

On the other hand, if it is a Java applet you are trying to run (remember, this means that one of its classes was marked as "extending" the class called Applet), the procedure is slightly more complicated. Java applets are designed to be integrated into HTML pages, and so to run them you need to insert them into such a page. Since all we really want to do with these applets is see how they work, we can use a simple "dummy" HTML page which contains no text or graphics, only the applet itself.

Such an HTML page would look like this:

<TITLE>applet test</TITLE>
This page requires Netscape 3.0 or higher on a Macintosh, 
and Netscape 2.0 or higher on a Windows95 PC.
<APPLET code="MyClassName.class" HEIGHT=200 WIDTH=200>

You can use this example as a template; just replace the "MyClassName" with the name of the applet you wish to run. (If your program involves more than one class, give the name of the one which is declared as "extends Applet"). The HEIGHT and WIDTH parameters describe the window in which the applet will run. You can change these numbers if you want.

Then, to see your applet in action, make sure that this file is saved in the same place as your .class files, and open it up in Netscape using the "Open File" command.

There's only really one flaw in this whole scheme, and that is this: Netscape refuses to acknowledge any changes you make to a Java applet when you open it again. Hence, if you want to change your applet and view the results of the new version, you must not only close the window containing the previous copy of your applet, you must in fact quit Netscape and run it again, and then use the "Open File" command to reopen the HTML file "containing" your updated applet.

If this gets to be too much of a pain in the neck, you can try using the program called appletviewer; however, as with the standalone Java interpreter, you will need to run appletviewer from a DOS shell. To do this, move the ".class" files containing your program and the HTML file which "contains" the applet all to the K: drive. Then run a DOS shell by choosing "MS-DOS Prompt" from the Start menu. When the prompt appears, type

k:\\"COS 111 Applications"\java\bin\appletviewer HTMLfile
where HTMLfile is the name of the HTML file which "contains" the applet you're trying to run. appletviewer will actually only run the applet itself, and will not show you the rest of the HTML page; other than this, it should be the same as running the applet under Netscape, except that you will not need to spend as much time quitting and restarting Netscape.