Tuesday, September 22, 2009

HOWTO specify default JVM arguments when using Java JAR manifest

A couple of times I've wanted to be able to specify a set of "default" JVM and program properties using the manifest file of a JAR file so that I could invoke it easily with "java -jar MyProgram." In my case, I wanted to set the JVM system property "https.proxyHost" so that all my web traffic would go through a local proxy.

Unfortunately, there doesn't appear to be a way to specify program arguments in a Java JAR manifest file.

To get around it, the solution is to use properties. I found a useful post here (http://www.velocityreviews.com/forums/t129370-vm-arguments-in-manifest.html) that suggests doing something like this:

static {
System.setProperty("https.proxyHost", proxyHost)
}

This solution actually worked quite well. I ended up writing a "JVMProxyHelper.setProxy(host,port)" routine that I simply invoked from the main() of the program I was running. If I get really fancy, I'll go back and make it read the property out of a file and register it that way, but this works for my quick need.

No comments:

Post a Comment