Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a safe, programmatic way to determine if it's safe to open a Swing window?
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <p>For instance, if I unset DISPLAY and run the application, I get:</p> <pre> 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] </pre> <p>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.) </p> <p>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.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload