Exporting jar with VM arguments

2020-06-27 06:47发布

问题:

I've written a Java application with Eclipse that uses SWT for the UI. (see SWT Exception when running jar: Exception in thread "main" org.eclipse.swt.SWTException: Invalid thread access for detail).

I exported as JAR and was having trouble getting it to run from Finder as well as from Terminal. I finally got it to run from Terminal with java -XstartOnFirstThread -jar CommonDenom.jar

I still, however, cannot get it to run when I export as a JAR from eclipse. This is because it needs the argument -XstartOnFirstThread bundled with it when it exports. Then I noticed that in Properties >> Run/Debug Settings >> commonDenom >> Edit >> (x)= Arguments there is a tick-box that says "Use the -XstartOnFirstThread argument when launching with SWT." But it was already checked. So I figured this option only applies when launching the code with Eclipse, and doesn't apply to the exported JAR.

So I added the argument manually to the VM Arguments box in the same tab. When I went to export as a runnable JAR, I noticed a warning that reads "VM arguments will not be part of the runnable JAR. Arguments can be passed on the command line when launching the JAR.

Ultimately, I need a way to get this to launch from finder (be it JAR or otherwise) without having to open Terminal and launch it manually. Yes, I can write a Shell script to launch it, but I feel there must be a simpler way.

回答1:

You have two options. The easy way is to create a shell script:

#!/bin/bash
java -XstartOnFirstThread -jar CommonDenom.jar

The user runs the script, which sets the arguments and runs Java.

The other way is to create an Application Bundle. It include a properties file (Info.plist) where you can set these properties. You can also use Oracle's appbundler tool to create an application bundle.