Sample Code

Current Qtr >> home

Sample Code

Building Your Own Executable Jar File

(I.e., Java's version of a stand-alone application program)

Asteroids.jar is a version of Asteroids that's been turned into an "executable .jar file", so that the Asteroids simulation can be run directly by a) double-clicking on it (in Windows) or b) running it from a command prompt by typing "java -jar Asteroids.jar".

To create the executable .jar file, I followed the following procedure:

  1. I added a "public static void main(String[] args)" method to Asteroids.java (the main class), which is what gets invoked when the application is executed.  In this case, I just had it invoke the existing static test method.
  2. I copied the uwcse.jar file to Asteroids.jar.
  3. I compiled all the .java files above to .class files, e.g. using BlueJ.
  4. I added the .class files to the Asteroids.jar file by typing at a command prompt "jar uf Asteroids.jar *.class".
  5. I wrote the following Asteroids.mf "manifest" text file, which identifies the main class in the application.  For some other application, I would write a slightly different .mf file to identify the main class of that application.
  6. I added Asteroids.mf to the .jar file by typing "jar ufm Asteroids.jar Asteroids.mf".

Check out the Sun JDK documentation on the jar tool for more information.  Also look at this page for information on manifest text files.

Building Your Own Applet

(I.e., making your Java application run inside a web browser)

To make a web-based interface to running the Asteroids application, I followed the following procedure:

  1. I wrote AsteroidsApplet.java wrapper class.  Look over the comments in that code to see a recipe for making your own applet wrapper class that works well with our graphics library classes.
  2. I built the Asteroids.jar file as described above, but including AsteroidsApplet.class.
  3. I wrote the Asteroids.html web page, including an APPLET HTML tag.  Look over the web page to see how to write your own APPLET HTML tags.
  4. I tested this by typing "appletviewer Asteroids.html" at a command prompt (assuming Asteroids.html and Asteroids.jar are both in the current directory).