tags:

views:

308

answers:

3

As it was made clear in my recent question, Swing applications need to explicitly call System.exit() when they are ran using the Sun Webstart launcher (at least as of Java SE 6).

I want to restrict this hack as much as possible and I am looking for a reliable way to detect whether the application is running under Webstart. Right now I am checking that the value of the system property "webstart.version" is not null, but I couldn't find any guarantees in the documentation that this property should be set by future versions/alternative implementations.

Are there any better ways (preferably ones that do not ceate a dependency on the the webstart API?)

+3  A: 

Use the javax.jnlp.ServiceManager to retrieve a webstart service. If it is availabe, you are running under Webstart.

See http://download.java.net/jdk7/docs/jre/api/javaws/jnlp/index.html

Tom
Thanks for the suggestion. I already figured this, but it turns that WS is not part of the standard JRE classpath and since I don't want to ship the API jar I prefer not to use it (and no, reflection is not the answer I am looking for.)
ddimitrov
Or you're using the new 6u10 plugin...
Tom Hawtin - tackline
@ddimitrov, why do you want to avoid reflection? If you get a `ClassNotFoundException` for `ServiceManager` you know that you are *not* running under JWS.
finnw
A: 

As you mentioned, checking the System property as follows is probably the cleanest way:

private boolean isRunningJavaWebStart() {
    return System.getProperty("javawebstart.version", null) != null;
}

In a production system I have used the above technique for years.

You can also try to check to see if there are any properties that start with "jnlpx." but none of those are really "guaranteed" to be there either as far as I know.

An alternative could be to attempt to instantiate the DownloadService us suggested by Tom:

private boolean isRunningJavaWebStart() {
    try {
        DownloadService ds = (DownloadService) ServiceManager.lookup("javax.jnlp.DownloadService");
        return ds != null;
    } catch (UnavailableServiceException e) {
        return false;
    }
}

Of course that does have the downside of coupling your code to that API.

Oleg Barshay
ServiceManager is still in the javaws.jar; Do you know if there's any official documentation stating the system properties that are set?
ddimitrov
I couldn't find anything in their documentation. API document does not specify any system parameters. I think if you want the "official" way of detectingWebStart you would have to use their API.
Oleg Barshay
There are JNLP implementations other than Sun's WebStart.
Tom Hawtin - tackline
While certainly true, original question asked about Sun's implementation.
Oleg Barshay
+2  A: 

I have no real experience with Java web start other than looking at it a few years back.

How about start your application with a parameter that you define than you set when the app is started via Java web start.

If you want to pass in arguments to your app, you have to add them to the start-up file (aka JNLP descriptor) using or elements.

Then check to see if these properties are set.

Again this is a suggestion I have not coded for JWS and it may not be this easy.

Paul Whelan
That is a workable option, but requires that the application modifies its jnlp file spreading the hack into multiple places.
ddimitrov
Or use a separate main.
Tom Hawtin - tackline