Lab 7
Page 5


Editing, Compiling and Running Java Applets


Editing Java Code

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". TextPad should be available under "Cluster Applications". If you cannot find it, ask for help from a lab assistant. 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 classname.java, where classname is the name of the class which that file defines. When you save a Java file in TextPad, choose the file type "java"; the ".java" extension will be added automatically.


Compiling Java Code

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 of Java virtual machine language code, which can be understood by a Java interpreter. This Java virtual machine language program is called "byte code".

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 Desktop, making sure that the ".java" extension appears as described above (ask a TA for help if the extension on the Desktop ends up as something else, or if you're not sure how to save to the Desktop). Then, from within TextPad, choose the "Java Compiler" command from the Tools menu. (If "Java Compiler" does not appear in the Tools menu, TextPad needs configuring. Ask a lab assistant.) 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 on the Desktop, where you 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 Applets

All the java that you will be creating in the lab are java applets. 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:

<HTML>
<HEAD>
<TITLE>applet test</TITLE>
</HEAD>
<BODY>
<P>
This page runs a Java applet.
</P>
<APPLET code="MyClassName.class" HEIGHT=500 WIDTH=800>
</APPLET>
</BODY>
</HTML>

You can use this example as a template; just replace the "MyClassName" with the name of the applet you wish to run. The HEIGHT and WIDTH parameters describe the window in which the applet will run. You can change these numbers if you want. When you save the html file, make sure the File Type is set to HTML in the Save As window.

Then, to see your applet in action, make sure that this html file is saved on the Desktop (the same place as your .class file), and just open your new html page in Netscape either by double-clicking the file on the desktop, or using Open File from the File menu of Netscape.


PREVIOUS 1 | 2 | 3 | 4 | 5 | 6 | 7 NEXT