有没有一种安全,可编程的方法来确定打开Swing窗口是否安全? [英] Is there a safe, programmatic way to determine if it's safe to open a Swing window?

查看:119
本文介绍了有没有一种安全,可编程的方法来确定打开Swing窗口是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户可以通过运行java -jar app.jar从命令行调用的java应用程序,或者通过双击.jar文件从桌面环境调用。

I have a java app that the user might invoke from the command line by running java -jar app.jar or from a desktop environment by double-clicking the .jar file.

我希望应用程序能够检测它是否可以安全地启动Swing GUI,或者是否必须使用命令行界面。如果我只是在没有检查窗口系统可用性的情况下开始调用Swing函数,那么Java就会出现InternalError。

I would like the app to be able to detect if it can safely launch a Swing GUI or if it has to use a command-line interface. If I just start calling Swing functions without checking for the availability of a window system, Java bails out with an InternalError.

例如,如果我取消设置DISPLAY并运行应用程序,我得到:

For instance, if I unset DISPLAY and run the application, I get:


java.lang.InternalError: Can't connect to X11 window server using '' as the value of the DISPLAY variable.
    at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
    at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:62)
    at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:178)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.X11GraphicsEnvironment.(X11GraphicsEnvironment.java:142)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
    at sun.awt.X11.XToolkit.(XToolkit.java:112)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at java.awt.Toolkit$2.run(Toolkit.java:849)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:841)
        [etc]

我可以尝试... c在那种情况下atch内部错误并运行命令行界面,但我听说不应该捕获错误,因为它们可能使JVM处于不一致状态。 (如果我错了,请纠正我。)

I could just try...catch the InternalError and run the command-line interface in that case, but I'd heard that one shouldn't catch Errors because they might leave the JVM in an inconsistent state. (Please correct me if I'm wrong.)

有什么方法可以检查我是否可以安全地打开Swing窗口而不会捕获InternalError? (我可以检查DISPLAY环境变量是否为空,但是非空的DISPLAY不能保证X服务器实际工作。这种方法也不适用于非X11环境。)

Is there any way that I can check to see if I can safely open a Swing window without catching an InternalError? (I could just check if the DISPLAY environment variable is not empty, but a non-empty DISPLAY is no guarantee that the X server actually works. Nor would that approach work in non-X11 environments.)

推荐答案


我可以尝试...捕获InternalError并在这种情况下运行命令行界面,但我听说过一个人不应该捕获错误,因为他们可能会使JVM处于不一致状态。 (如果我错了,请纠正我。)

I could just try...catch the InternalError and run the command-line interface in that case, but I'd heard that one shouldn't catch Errors because they might leave the JVM in an inconsistent state. (Please correct me if I'm wrong.)

你的理解是对的。

捕获错误并尝试恢复/继续运行是一个坏主意,因为JVM可能(已经)处于无法进行恢复的状态。

It is a bad idea to catch an Error and attempt to recover / continue running because the JVM could (already) be in a state from which recovery is not possible.

但是,捕获特定错误,向控制台打印信息性诊断并立即退出JVM是一件非常安全的事情。 (System.out / err流对于导致抛出错误的大多数事情非常有弹性。)

However, catching a specific error, printing an informative diagnostic to the console and immediately exiting the JVM is a pretty safe thing to do. (The System.out / err streams are pretty resilient to most things that cause an Error being thrown.)

在这种特殊情况下,你可能无法恢复。无论如何,拯救是最明智的选择。 (在尝试启动GUI之前调用 isHeadless()虽然是一个更好的主意。)

In this particular case, there's probably nothing you could do to recover. So bailing out is the most sensible option anyway. (Calling isHeadless() before you attempt to start the GUI is a better idea though.)

这篇关于有没有一种安全,可编程的方法来确定打开Swing窗口是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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