This document explains how to use the JDK applet viewer to run Java applets.
This is an alternative to using a Web browser. I would suggest that you use the
applet viewer for all routine development and testing of Java applets. I assume
that you have already entered and compiled the applet program, and now wish to
run it.
Why use the applet viewer (and not a Web browser)?Java applets are designed to be embedded in a Web page, and the natural way to run them is to use a Web browser. However, there are a number of good reasons not to use a Web browser during the development of Java applets.
Using the appletviewerIt is important to remember that Java applets are intended to be viewed in a Web browser, and the input to the applet viewer is an HTML file. HTML is the format for specifying the content of Web pages. In practice the appletviewer will only process that part of the HTML file that contains applet information, so you don't need to create `real' Web pages at all. There are two ways to use the applet viewer, which I will refer to as the `correct' method and the `short' method. In practice the short method is usually quicker and easier to use, but you should make sure you understand the `correct' method first. Reading about the short method first may well lead to misunderstandings about what is going on.In both cases, I will assume that you have written and compiled an applet, and it defines a class called `Hello1'. The source file is called `Hello1.java' `Correct' methodThe `correct' method is to create an HTML file with the applet information in it, and feed it into the appletviewer program. If you know how, you can create a complete HTML file if you wish, but it is unnecessary. HTML files are essentially text files, so I recommend that you use Windows `Notepad' to create the HTML file. Do not use a word processor. I also recommend that you start it at the command prompt, in the same directory as the Java program. So if the file `Hello1.java' was in directory `\Hello', the command prompt session would look something like this:
<applet code=Hello1 width=300 height=300></applet> This is the standard notation for specifying an applet in an HTML file. The statment `code=Hello1' is an indication to run the Java class `Hello'. Unless you specify otherwise, the applet viewer will assume that the class file is in the same directory as the HTML file. Having created the HTML file, we can run the applet using the appletviewer like this:
`Short' methodIn practice we don't need to create a separate HTML file. The applet viewer will look in any file you specify; all it's looking for is the text `<applet...>', and it will ignore anything else. You can use any text file for this job.Now you already have a text file associated with the Java applet: the Java source program itself. In the example above this was the file Hello1.java that you compiled. So if you put the line
<applet code=Hello1 width=300 height=300></applet>
appletviewer Hello1.java The problem here is that `<applet...>' is not proper Java. If you need to compile this program again you won't be able to, because the compiler will complain about errors. The solution is to make the `<applet...>' line a comment in the program. Comments are ignored by the compiler and are there to give guidance to the reader. One way of signalling a comment is to start a line with `//'. So in summary, we can make the applet viewer load the applet by adding the line //<applet code=Hello1 width=300 height=300></applet> to the file Hello1.java, then typing appletviewer Hello1.java The Java compiler will ignore the applet line altogether, and the applet viewer will ignore the rest of the Java program. This is a very quick and efficient way of running a Java applet. But be careful: when you type appletviewer Hello1.java it looks as though you are telling the applet viewer to run the file `Hello1.java', which is not true. We have put the applet information in Hello1.java entirely for convenience; it could have gone in any file. The applet viewer simply extracts the applet information from the file, and executes the applet that was compiled earlier. We still have to use javac to compile the Java applet. A note about the Java `plug-in'If you want to use a modern version of Java, but still require your clients to use your applet with a Web browser, it is now possible to make use of a `Java plug-in' for use with a Web browser. The plug-in is an add-on component for the Web browser that processes Java applets itself. This bypasses the normal Java support that the Web browser offers. If you get a plug-in from Sun, or that is endorsed by Sun, then you can be reasonably sure that it supports the Java version you are using. The disadvantage of this approach for commericial software delivery is that it requires your clients to obtain and install the plug-in, which may be undesirable. For more information about this, see Sun's Java plug-in page |
©1994-2003 Kevin Boone, all rights reserved