Making Applets


An applet is a program (written in java) that runs in a web page. Thus, there are two computers involved. The server, where the web page and applet come from, and the client, where the web page is displayed and the program runs.

There are 5 main steps to making an applet:
  1. Writing the source code
  2. Compiling the source code
  3. Making the code into a .jar file
  4. Adding the applet to a web page
  5. Putting your web page on the Internet

1. Writing the Source Code

Your program must extend the Applet class. Because of this, it also must conform to a whole list of security rules. These rules ensure that your applet will not do any harm.

Basically, your applet will not be able to: If you absolutely cannot avoid doing something forbidden, you can get your applet signed. This removes the security rules, but anyone who used it will have to go through a confirmation screen asking if they trust your digital signature. It is best avoid signed applets whenever possible.

Java source code files normally have the file extension .java, although this is not strictly neccesary.

2. Compiling the Source Code

A compiled java file is a .class file (the file extension does matter). Each java file will produce one or more .class files when compiled; all of these files are valuable.

Note that a .class file is not actually fully compiled until it is run. Instead, it is translated into bytecode, which is basicly a system-independant assembly language. When the java applet (or program) is run, the bytecode is translated into machine code by a Just-In-Time (JIT) compiler. This means that a .class file cannot be run directly, and that even a comparatively simple program can involve tens or hundreds of .class files.

The Java compiler on Unix (Hercules) has weird rules about packages and directory structures. Specifically, the folder your source files are in MUST have the same name as your package. At the top of each java source code file is a line that says

package PackageName;

where PackageName is the name of your package. The compiler uses these to group the files into packages (modules), with all files with the same package name in the same package. If you do not specify a package name, your file is put in the default package, which can be troublesome. Avoid it.

To compile your applet on Unix (Hercules):

Go to the directory immediately above the one with your source code. Then enter

javac PackageName/*.java

where:

3. Making the code into a .jar file

A .jar file is a way of collecting a whole bunch of seperate files together, sort of like a .zip file. Like a .zip file, a .jar file can be compressed, but the ones used for applets usually are not.

For an applet, you want to collect all your .class files and any images, sounds, data files, etc. your applet uses into a .jar file.

The main advantages of storing your applet in a .jar file are: To collect all your .class files into a .jar file on Unix (Hercules):

Go to the directory immediately above the one with your .class files. Then enter

jar JarName.jar PackageName

where:

4. Adding the applet to a web page

Generally, an applet must be run through a web page. Some compilers have an applet viewer associated with them, but this is not universal. For example, applets cannot be run from Unix (Hercules).

An applet is placed in a web page using the HTML <APPLET> tag:

<APPLET
	width=640
	height=480
	code="PackageName.AppletClass.class"
	archive="JarName.jar"
	alt="You need to enable Java">
	Sorry, your web browser does not support java.
</APPLET>

where: The alt="You need to enable Java" line indicates that the text You need to enable Java should be displayed if the applet itself cannot be displayed for some reason, such as if the user's browser settings are set to not display applets.

The Sorry, your web browser does not support java. line indicates that the text Sorry, your web browser does not support java. should be displayed if the user's web browser does not even know what an applet is. This might occur with really old web browsers.

According to the World Wide Web Consortium (W3C), the official authority on HTML, the <APPLET> tag should be replaced by the more general <OBJECT> tag. However, the <OBJECT> tag is confusing to work with and Sun Microsystems, the inventors of java, advise that you use <APPLET>.

5. Putting your web page on the Internet

Luckily, if you are at the University of Regina, this step is not too hard; the University will host a web site for you from your Hercules account.

To set up your University web page:
  1. Go to your home directory
  2. Create a directory called public_html.

    mkdir public_html
    

  3. Create an index.html file in the public_html directory. This file will be displayed by default when someone goes to your web site.

    Here is a default index.html file you can use. It already has the <APPLET> lines shown above in it. If you download the file, you will have to change the name from index.txt to index.html in order to use it as a web page. Make sure to put in the correct .jar file and applet!

  4. Return to your home directory
  5. Set the visiblity permissions on your home directory to rwx--x--x.

    chmod 711 .
    

  6. Set the visiblity permissions on the public_html directory to rwxr-xr-x.

    chmod 755 public_html
    

  7. Set the visiblity permissions on everything in the public_html directory to rw-r--r--.

    chmod 644 public_html/*
    

Your website should now be on the Internet at "http://www2.cs.uregina.ca/~YourUserName/". You can access your applet by displaying the web page in (almost) any web browser. If you use Internet Explorer 7 or later, it will automatically block your applet from running for "security reasons". There may be a bar at the top of the display area that you have to click, a warning message, or whatever else Microsoft dreams up. You will almost certainly have to click on your applet before it starts.

To rerun your applet, reload/refresh the web page.

If you change your applet, you will have to restart the browser to see the new version. It only works if you close all tabs and windows. Because of this, it may be a good idea to open two differant web browsers (e.g. Internet Explorer and Firefox) and use one to look things up in and the other to display your applet.


Back to miscellaneous stuff page
Back to home page