# How to write and deploy a Java applet

A Java applet is a special kind of Java program that a web browser enabled with Java can download from the internet and run. An applet is typically embedded inside a web page and runs right in the browser window.

### Steps

1. In Eclipse, expand the project folder and package into which your applet should be placed. In the text that follows, this project folder will be named "Paul Oser", and the package will be named "edu.emory.oxford.paul.oser", but your project and package names will likely be different -- just modify things accordingly...

2. Right-click on the package icon (e.g., "edu.emory.oxford.paul.oser") and select "New->Class". Name the resulting new class "HelloApplet".

3. Replace the java code found in your HelloApplet class with the code below. Select "Run As->Java Applet" from the Run menu, to verify the applet works. The applet should prompt you to enter your name, and then print a message of the form "Hi ! It is good to meet you!".

package edu.emory.oxford.paul.oser;

import acm.program.*;

public class HelloApplet extends ConsoleProgram {

public void run() {
println("Hello " + name + "!  It is good to meet you!");
}
}


4. To deploy an applet, you will need to house it somewhere on the internet. Any easy way to do this is through the use of the free cloud-based storage application "Dropbox". If you don't already have it installed, download and install Dropbox (www.dropbox.com).

5. Now, in Eclipse, select "File->Export..." and highlight the "JAR file" option found in the "Java" folder shown in the resulting dialog box, and click "Next".

Select the project folder (e.g., "Paul Oser") in the next dialog box and make sure the corresponding ".classpath" and ".project" options are checked. Make sure "Export generated class files and resources" and "Compress the contents of the JAR file" are checked.

(Note, these directions will actually export ALL of the classes in your project folder to this jar file -- not just the HelloApplet class. This is fine for now -- but if you wanted to only export some of the classes in your project folder, you could expand the project folder item in this dialog box and navigate to your package (e.g., Paul Oser -> src -> edu.emory.oxford.paul.oser) and then check the classes you want to export when they are displayed on the right.)

6. Click the "Browse..." button and select a place to save your archive file. Give the archive file an appropriate name (e.g., "paul.oser.jar") and click "Save". Finally, click "Finish" to create the JAR file. You may be notified of some compile warnings when this happens -- you may ignore these.

7. Open Dropbox, and copy the jar file to the "Public" folder inside your main Dropbox folder. Notice the applet above references the acm.jar library. Since this is not a standard java library, you will also have to copy the acm.jar file to your public folder.

8. Now right-click your jar file (e.g., "paul.oser.jar") and choose "Dropbox->Copy Public Link".

Paste this link somewhere (like an empty text file) so that you can inspect it. Note it will take on a form similar to the following: "https://dl.dropbox.com/u/18254111/paul.oser.jar". The first part of this URL, "https://dl.dropbox.com/u/18254111/", is called the codebase for the applet -- essentially, this tells your browser where your applet's code is located. Copy down the text of your applet's codebase (you will need to refer to it in the next step).

9. Copy the following into a new text file.

<html>
<title>
HelloApplet by Paul Oser
</title>
<body>
<h2>HelloApplet by Paul Oser</h2>
<applet
codebase="https://dl.dropbox.com/u/18254111/"
archive="paul.oser.jar, acm.jar"
code="edu.emory.oxford.paul.oser/HelloApplet.class"
width=550 height=300>
</applet>
</body>
</html>


Replace both occurrences of "Paul Oser" with your name. Then, importantly, modify the line

codebase="https://dl.dropbox.com/u/18254111/"

to reflect your applet's codebase, and modify the lines

archive="paul.oser.jar, acm.jar"
and
code="edu.emory.oxford.paul.oser/HelloApplet.class"

to reflect the names of your jar file(s) and package. Note how the code attribute is specified. Essentially, it is the package name followed by a "/", the class name, and ".class".

Save the modified text file with a name similar to "paul-oser-hello-applet.html" in the public folder of your main Dropbox folder.