Starting a class with One-JAR that’s not your Main-Class

I use One-JAR to package a number of .jars from different project into a single jar file which is my distributable application. Within that jar file are a multitude of utility classes. To run my application I use

java -jar myonejar.jar

which launches my application’s main class com.firtl.MainClass just fine. I’d also like to be able to run some utility classes from the same jar like this:

java -cp myonejar.jar com.firtl.UtilityClass input1.bin input2.bin

This doesn’t work because One-JAR produces a jar file for an application class com.simontuffs.onejar.Boot – your own application’s jar file ends up in a /lib ‘directory’ in the jar file. You need to tell the Boot class to load and execute your utility class. You can pass command-line ‘switches’ to the Boot class via the Java system property facility. One-JAR supports a few, some useful help text is available by:

java -jar myonejar.jar --one-jar-help

The system property you need to set is one-jar.main.class – make sure you set the property before -jar:

java -Done-jar.main.class=com.firtl.UtilityClass -jar myonejar.jar input1.bin input2.bin

One-JAR’s Boot class will load UtilityClass and pass {“input1.bin”, “input2.bin”} to its main method. Easy once you know how!

Leave a Reply

Your email address will not be published. Required fields are marked *