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

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

问题描述

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

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

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

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.

有没有更好的方法(最好是不依赖 webstart API 的方法?)

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

推荐答案

当您的代码通过 javaws 启动时,javaws.jar 被加载并且您不想依赖的 JNLP API 类可用.您可以查看 JNLP 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;
}

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

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

或者,您可以切换到使用 javaws.jar 进行编译并捕获 NoClassDefFoundError:

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;
    }
}

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

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天全站免登陆