Lab 7
Page 3


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 (class is the term used for a type of object); 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. While your are working, save files on the Desktop; only copy them to your public_html directory when you are done or if you are going to take a break and log off for a while.


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). TextPad contains a "Compile Java" command in the Tools menu. However, because you will actually need to combine several files (most provided by us) when you compile your sorting program, you need a command to compile all java files on the desktop. If the command "Compile All Files" does not appear in the Tools menu, follow these somewhat inscrutable directions to add it:

  1. In your TextPad editor, go to the Configure menu and select Customize
  2. Select the Tools tab
  3. Select New and then Command
  4. Type in "c:\java\bin\javac" in the dialog box for the filename and click OK
  5. In the Menu Text section type "Compile All Files"
  6. In the Parameters section type "*.java"
  7. In the Initial Folder section type "$FileDir"
  8. Switch ON the option for Capture Output
  9. Click OK.
Then, whenever you want to compile your Java code, pick the newly-installed Compile All Files option from the Tools menu. (If you encounter any problems with these directions, request the assistance of a lab assistant.) This will automatically invoke the Java compiler program and run it on the ".java" files you have 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 new files on the Desktop, where you saved your original source code file -- one file for each ".java" file that you had. Each new file will have the same name as its corresponding ".java" file, except that it will end in ".class" rather than ".java". The files contain the byte code version of the Java code.


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.

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 use applet viewer under the Tools menu in TextPad, called "explore".


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