I’m writing a small program and I’m packaging it in an executable JAR file so that all the end user has to do is double-click on it to run it. None of that command-line madness that… okay, that’s another rant. ANYWAY, my particular program uses an external package that’s ALSO stored in a jar file. To put everything in one JAR means putting a jar inside another jar.
That part is easy.
Ah, but I still want my main jar file to be executable. Still easy.
AH, but I want it to run anywhere on the destination file system, without the end user having to dick mess around with CLASSPATH statements and other crap, trying to point my application to a jar file that’s contained in its own archive. Nor do I want to spend time setting CLASSPATH statements INSIDE the jar file, because a) I have more important things to do and b) they don’t work as advertised.
Fortunately, there’s a solution.
I’ve had a lot of success using ONE-JAR.
ONE-JAR is an open source solution for jar-inside-jar packaging. It works. It’s free. And it works. (and its free!)
That’s really all you need to know.
Ummm…actually, you need to know a little bit more….
Like how to use the damn thing, because my first experience with one-jar took me a LOT longer than it should have. Either there aren’t any step-by-step instructions on how to use it, or said instructions are hidden away deep in the otherwise-informative documentation. So, for the benefit of my fellow Java coders, here it is:
Using ONE-JAR to package a simple Java Application
- Create and test a Java application.
- Create an executable JAR file with your application’s CLASS files in it. Name this “main.jar”
jar cfm main.jar manifest.txt *.class - Create three directories: MAIN, LIB, and BOOT
- Place your “main.jar” file in the MAIN directory.
- Place the jar files that your main application depends on in the LIB directory.
- Create a new JAR file out of the MAIN and LIB directories. Name this one “application.jar”. You do not need to add a manifest or do anything special to this file. It does not need to be executable. Just make it so that it contains the contents of the MAIN and LIB directories.
jar cf application.jar main lib - Extract the contents of the “one-jar-boot.jar” file into the BOOT directory.
- Navigate to the BOOT directory, and update the “application.jar” file with the following:
jar -uvfm ../application.jar boot-manifest.mf *.* - Your “application.jar” file should now be executable. Test it.
- Test it again: Move the “application.jar” file to another location… or better yet, another computer. It should run there, too.
- You’re done.
Note: The above step-by-step instructions assume that creating JAR files isn’t complete voodoo to you, and that you know what the actual command-line instructions are DOING. It’s meant to be instructions on using a specific open-source package, NOT instructions on how to package code in a jar file.
Note, Again: I’ve had a lot of luck packing this process in a batch file (on Windows) and running the whole thing with a simple double-click. Naturally this means I’ve extracted the contents of the “one-jar-boot.jar” archive into the correct place ahead of time. The contents of an actual .BAT file that I use are provided here for an example:
jar cfm main.jar manifest.txt *.class
copy main.jar main\main.jar /y
jar cf FlowWatcher.jar main lib
cd boot
jar -uvfm ../FlowWatcher.jar boot-manifest.mf *.*
DAMN with the NOTES!: Obviously you don’t need to NAME your files the same thing I named mine (unless you are just cutting-and-pasting my BAT file, which you shouldn’t be doing anyway). Name your program and its archive anything you want. I shouldn’t have to say this, but you know how people are…
Popularity: 17% [?]
Entries (RSS)
July 23rd, 2007 at 7:30 am
[…] in the title of this article. If you are doing something fancy with classpaths or JAR files inside other JAR files, then obviously you need a higher level of understanding, and the instructions aren’t nearly […]
April 2nd, 2008 at 2:28 pm
Use http://www.jdotsoft.com/JarClassLoader.php as an alternative. It’s much simpler.
August 7th, 2008 at 12:45 am
[…] and i’ve used this tutorial on how to use it step by step: http://www.technowledgebase.com/2007/07/10/java-using-one-jar/ P.S. you have to download this One-jar SDK: one-jar-boot-0.96.jar. and you have to add another […]