An executable jar file has a defined default class, allowing the default class to be invoked without explicitly identifying it via the command line. On a supporting OS (such as Mac OS/X or Microsoft Windows), this allows the class to be invoked by double-clicking the file.
I know that making a jar file executable requires an entry in the manifest file. I can usually remember that the attribute in question is Main-Class (although I’m usually fuzzy about the proper capitalization). The problem is remembering the correct command line options to insert my own MANIFEST.MF file into a jar file. All in all, it is much simpler to add a manifest element and let ant handle it for me.
Let’s start with the example from my Ant Hello World Revisited. I’ve added a manifest element that specifies “hello” as the default class to be executed. Note that I’ve also designated the “jar” task as the default task.
$ cat hello.xml
As a reminder, here’s the java class:
$ cat hello.java
Now we simply build our jar file:
$ rm *.jar
And execute the default class by using the -jar command line option to java:
$ java -jar hello.jar
Disclaimer: I don’t claim to be an expert on ant. Please send comments and corrections.