Friday, February 25, 2011

Java : Comment Applet Vous? (Part 2)

Today's post will be the dullest and nerdiest ever. Unless you need to post a "jarred" ("jarchived"?) applet right now, just skip to the pictures at the end. We're only posting this for our own benefit, as creating a jarred applet is the kind of tedious, fiddly, counter-intuitive task that's painful to learn but very easy to forget.

Still with us? Okay, let's reprise what we covered last time:
  • We converted our "PeekaBoo" stand-alone application ("App"), into "PeekaBooApplet".
  • So, "PeekaBoo", an extension of JFrame, became "PeekaBooApplet" an extension of Japplet.
  • Our constructor method was renamed "init()" and was given type void.
  • Our "paintComponent" method was renamed "paint".
  • We were able to delete the "main" method entirely.
  • We then added HTML code to our post to invoke the applet.
  • The HTML included a codebase clause to tell our browsers where to look for the applet and data.
Today, we're going to bundle our applet program (".class") together with its data (two ".jpg"s) into a Java archive (".jar") file. the steps required are:
  • Minimal code changes.
  • Create a "mainClass.txt" file to identify the main entry point of the applet.
  • Create a DOS batch file "makeJar.bat" to automate the archiving process.
  • Modify our HTML to tell our browsers where the archive is stored.
First code change, we like to stick a "Jar" at the end of our class name to avoid confusing ouselves. (We must remember to save the file as "PeekaBooAppletJar.java".)
public class PeekaBooAppletJar extends JApplet implements MouseListener
In our "init()" method, we have to change the pathnames given for the image files from a relative pathname in a file-system to an absolute pathname within the ".jar".
(In practice, this usually just means sticking a "/" at the front.)
images [PEEKA] = loadImage("/Images/peeka.jpg");
images [BOO]   = loadImage("/Images/boo.jpg");
That's it for code changes - told you they were minimal.

We now create a text file "mainClass.txt" which passes information about the main entry point of our applet into the archive making program ("jar"). This is known as a manifest file.
Main-Class: PeekaBooAppletJar

One point to note there has to be a newline at the end of the "Min-Class:" line. We've no idea why - just stick a blank line at the end of the file.

We next need a batch file - "makeJar.bat" - to automate the process of making the ".jar".
@echo off

echo Making PeekaBooAppletJar jar...
jar cvmf mainClass.txt PeekaBooAppletJar.jar PeekaBooAppletJar.class Images
echo.

echo Indexing jar...
jar i PeekaBooAppletJar.jar
echo done

A ".jar" file is just like a ".zip" file (you can even open a ".jar" with "WinZip").
In our batch file, we're calling the "jar" utility twice. The first call is with flags
  • c - create a new ".jar".
  • v - verbose output.
  • f - write the output to the specified ".jar" filename. (PeekaBooAppletJar.jar".)
  • m - include manifest information from the specified manifest file. ("mainClass.txt".)
The second call is with flag
  • i - create an index in the ".jar" of all the jars in the manifest.
This second step probably isn't necessary for our simple example but we never, ever intend to read the manual for "jar", so we'll never find out.

One last step is to add an archive clause to our HTML.
<applet
 code="PeekaBooApplet.class"
 archive="http://mgt.yourhda.com/Java/PeekaBooAppletJar.jar"
 codebase="http://mgt.yourhda.com/Java/"
 width="237"
 height="315">
</applet>
Putting it all together:

  • Copy yesterdays "PeekaBooApplet.java".
  • Make the above simple changes and save as "PeekaBooAppletjar.java".
  • Compile using javac.
  • Run makeJar.bat.
  • Copy the resulting "PeekaBooAppletJar.jar" to your server.
  • Modify the above HTML to point to your server and save to a blog post or ".html" file.




Click on Catwoman and move the mouse away to test.


That's quite enough on applets.

We'll give it a rest for a while. When we come back: playing MP3s in Java.

12 comments:

  1. Haha so that's what’s really under cat woman's mask!

    ReplyDelete
  2. Dogs doesn't know what applet is

    ReplyDelete
  3. Hopefully I'll be able to code for myself after reading all these tuts. glad I'm following you.

    ReplyDelete
  4. hitler clapping is brilliant

    ReplyDelete
  5. Eh gads man! That's quite a pile of code you have there.

    ReplyDelete
  6. A job well done, sir. You are thanked for your time here.

    ReplyDelete
  7. Nice tutorial. Might start adding some applets to my site.

    ReplyDelete
  8. It's the playing mp3s in java I'm really interested in... quite excited for that one.

    ReplyDelete
  9. I'm stealing that hitler clap for future use! thanks.

    ReplyDelete