"Going Pro"

Making a Double-Clickable Application

Computer Science I
Media Computation

Going Pro: making a Double-Clickable .jar File

Last session we created a neat little FoxTrotDownloader for me. To run the program, though, I have to run a Java program.

    > FoxTrotDownloader app = new FoxTrotDownloader();
    > app.getTodaysCartoon();

If I want to save myself typing this every day, I can add a main() method to FoxTrotDownloader.java and then just run the compiled class.

That is great for you and me, but what about our friends, family, and neighbors who also love FoxTrot? Even I would prefer to be able to just double-click on an icon and see today's comic!

How you can you make a double-clickable application from your Java program?

Many industry-grade development environments include tools for creating double-clickable apps. For example, the developer tools in Mac OS X enable a programmer to create apps, design custom icons, and write all sorts of scripts for interacting with the OS. Unfortunately, the IDEs you are mostly likely to know at this point, such as Dr. Java, do not. Nor do vi or emacs.

Many of the professional tools are quite complex. Learning them just for the sake of creating a double-clickable app seems too expensive for the pay-off.

But, if you do not make a double-clickable app, your program will be hard or impossible for many others to use. Your mom and dad, or grandma and grandpa, or five-year-old cousin, probably don't know how to fire up the Java compiler and Java compiler. They probably don't know how to fire up Dr. Java, either. And they don't want to learn -- nor should they need to.

Therefore, use the Java Development Kit's jar tool to create a double-clickable Java archive file.

The jar tool packages a bunch of files into a single archive file that you can give to another or offer for download on the web. If we tell the archive which class file contains the main() method, then users will be able to double-click on the file's icon and run it like a standalone application. The file that tells the archive this information is called a manifest file. The manifest acts like a "read me" file for the Java virtual machine can read. When we run jar, it automatically creates a manifest file for the archive. Unfortunately, the default manifest file does not include a definition of the class that contains the main() method.

Here are the steps you can use to create a double-clickable jar file. I'll illustrate them by creating a double-clickable FoxTrot downloader application.

  1. Create a manifest file for your application. You can download this template file and edit it. Change the string NAME-OF-CLASS-FILE-CONTAINING-MAIN to be the name of the class file that contains your main() method.

    For example, I changed the file to read:

                 Manifest-Version: 1.0
                 Created-By: 1.4.1
                 Main-Class: FoxTrotDownloader

    because FoxTrotDownloader.java contains the main() method that I want to execute.

  2. Place this file in the directory that contains your *.class files.

    You can move the manifest file to your bookClasses directory. I copied all of my .class files to a new directory named foxtrot-downloader and then moved my manifest.mf into that directory.

  3. Go to your Linux shell (or perhaps Windows DOS window). Change into the directory containing your *.class files and manifest.mf.

    I changed into my directory named mastermind/exe/:

                 cd ~/Desktop/foxtrot-downloader/

  4. Use the jar command to create your archive. The command you need is:
                 jar cfm jarFileName manifest.mf *.class

    where jarFileName is what you want to call your archive.

    To create my archive, I entered:

                 jar cfm FoxTrotDownloader manifest.mf *.class

  5. Move your jar file to whatever location you desire.

    I moved mine to my sessions/ folder for today:

                 mv FoxTrotDownloader ~/home/teaching/cs1/sessions/session25/

Voilá! And there you have it. Double-click on FoxTrotDownloader.jar and watch it go.

Making a Custom Icon

It's also fun to make your own icon for your double-clickable app. The standard "jar file" icon is so boring!

the standard jar file icon in Mac OS X

Industrial-strength IDEs often provide tools that help us do this. jar does not.

I know that this is possible in Linux and Windows, but I haven't done it in either of these systems in a while. But in Mac OS X I simply open the double-clickable file's Information window and paste the image I want in place of the icon. So I did this:

  1. ... ran my downloader to get today's comic:

    the FoxTrot comic from 11/28/06

  2. ... clipped out the colored peg portion of the interface:

    a square image of Jason and his iguana

  3. ... pasted that image into the .jar file's Information window. Now I see this on my desktop:

    my downloader icon

Of course, we spent several weeks learning how to manipulate images in Java, so I am sure that you all could come up with a cooler icon by opening a Picture, sclaing it, and processing it however you like.

You can read more about jar by typing "man jar" at your Linux prompt, or by visiting the JDK documentation.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... November 28, 2006