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.
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.
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.
I changed into my directory named mastermind/exe/:
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
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!
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:
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.