检测Webstart是否启动应用程序的最佳方法是什么? [英] What is the best way to detect whether an application is launched by Webstart

查看:80
本文介绍了检测Webstart是否启动应用程序的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在中所阐明的那样最近的问题,Swing应用程序在使用Sun Webstart启动程序(至少从Java SE 6开始)运行时需要显式调用System.exit()。



<我想尽可能地限制这个hack,我正在寻找一种可靠的方法来检测应用程序是否在Webstart下运行。现在我正在检查系统属性webstart.version的值是否为空,但我在文档中找不到任何保证,该属性应由未来版本/替代实现设置。



有没有更好的方法(最好不要停止对webstart API的依赖?)

解决方案

当您的代码通过javaws启动时,将加载javaws.jar,并且您不想依赖的JNLP API类可用。您可以改为查看是否存在JNLP API类,而不是测试不存在的系统属性:

  private boolean isRunningJavaWebStart(){
boolean hasJNLP = false;
try {
Class.forName(javax.jnlp.ServiceManager);
hasJNLP = true;
} catch(ClassNotFoundException ex){
hasJNLP = false;
}
返回hasJNLP;
}

这也避免了在编译时需要在类路径中包含javaws.jar。



或者你可以改用javaws.jar编译并捕获NoClassDefFoundError:

  private boolean isRunningJavaWebStart(){
try {
ServiceManager.getServiceNames();
返回ds!= null;
} catch(NoClassDefFoundError e){
return false;
}
}

使用ServiceManager.lookup(String)和UnavailableServiceException是麻烦因为它们都是JNLP API的一部分。未记录ServiceManager.getServiceNames()以进行抛出。我们专门调用此代码来检查NoClassDefFoundError。


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?)

解决方案

When your code is launched via javaws, javaws.jar is loaded and the JNLP API classes that you don't want to depend on are available. Instead of testing for a system property that is not guaranteed to exist, you could instead see if a JNLP API class exists:

private boolean isRunningJavaWebStart() {
    boolean hasJNLP = false;
    try {
      Class.forName("javax.jnlp.ServiceManager");
      hasJNLP = true;
    } catch (ClassNotFoundException ex) {
      hasJNLP = false;
    }
    return hasJNLP;
}

This also avoids needing to include javaws.jar on your class path when compiling.

Alternatively you could switch to compiling with javaws.jar and catching NoClassDefFoundError instead:

private boolean isRunningJavaWebStart() {
    try {
        ServiceManager.getServiceNames();
        return ds != null;
    } catch (NoClassDefFoundError e) {
        return false;
    }
}

Using ServiceManager.lookup(String) and UnavailableServiceException is trouble because both are part of the JNLP API. The ServiceManager.getServiceNames() is not documented to throw. We are specifically calling this code to check for a NoClassDefFoundError.

这篇关于检测Webstart是否启动应用程序的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆