Running Java Programs

4 Running Java Programs

Applications

Java applications are the easiest to run. You use the Java interpreter called, oddly enough, java. So, if your .class file created during compilation is called lab1a.class, then you would type java lab1a. If the application takes command line arguments, like the one in the on-line Java tutorial, you would type them after the class name, such as java Newton 2. Notice that you do not type the .class extension of the file, but you do have to type the .java extension when you compile.

Applets

Java applets are a little more bothersome to run. As mentioned before, the Java interpreter in this case reads an HTML file that points to the .class file. An HTML file typically ends in .html, and you should put the .class file in the same directory as the .html file that points to it.

In your labs, you will probably be provided with the .html file you need, but if you don't have it, here is a dummy one that will likely work for you.

<HTML>
<BODY>
<APPLET CODE="MyClass.class"> </APPLET>
</BODY>
</HTML>
Put the above text in a file that ends in .html, such as java.html. You should change the word MyClass.class to whatever your .class file is.

Now, with your .class and .html sitting in the same directory, you are ready to point a web browser at it. With Netscape, you can start it from the command line with netscape java.html with java.html replaced with your .html file. If you have Netscape already running, you can open it with Open File ... under the File menu.

There is also a program just for viewing Java applets called appletviewer. Start it with appletviewer java.html &, again with your .html file instead of java.html.
Next 5 The Newton's Method Example


David Maxwell, who is still writing this, would like to hear your comments and suggestions.