13

I have an application that needs user input for password.

What i want to do is to either read the password from console (if the OS supports one e.g unix) or display a JOptionPane and ask the user to enter his password (if the OS supports graphical interface e.g windows).

Some people may argue that a console will always be available in both the above cases so a console input will be sufficient. But problem is if the Java app starts using javaw.exe then a console is not available. Thus, i need a way to determine if i can do either case.

My problem is how to determine if the environment that the application runs from supports either a console or a graphical interface.

I know that a static method exists GraphicsEnvironment.isHeadless() but from the Java doc i think that this method cannot distinguish if the OS supports graphics, but rather than if the OS can support one of the I/O devices (keyboard, mouse, screen).

Does anyone know more about this? Am i able to retrieve if the OS supports console or graphics environment?

Thanks in advance.

nikkatsa
  • 1,751
  • 4
  • 26
  • 43
  • 1
    See [Is there a safe, programmatic way to determine if it's safe to open a Swing window?](http://stackoverflow.com/questions/5893236/is-there-a-safe-programmatic-way-to-determine-if-its-safe-to-open-a-swing-wind) – devnull May 17 '13 at 13:44
  • @devnull thank you for your answer. So actually, as the other post suggests, the isHeadless() method will do the job. I must have mis interpreted the Java documentation then. However, i ll try to find a unix machine to run the app and i ll post my findings back here. – nikkatsa May 17 '13 at 13:59

1 Answers1

21

GraphicsEnvironment.isHeadless() will return true in case:

  • the system property java.awt.headless has been set to true
  • your are running on a Unix/Linux system and there is no DISPLAY environment variable set

Here is the code that is used to retrieve the headless property:

    String nm = System.getProperty("java.awt.headless");

    if (nm == null) {
        /* No need to ask for DISPLAY when run in a browser */
        if (System.getProperty("javaplugin.version") != null) {
            headless = defaultHeadless = Boolean.FALSE;
        } else {
            String osName = System.getProperty("os.name");
            headless = defaultHeadless =
                Boolean.valueOf(("Linux".equals(osName) || "SunOS".equals(osName)) &&
                                (System.getenv("DISPLAY") == null));
        }
    } else if (nm.equals("true")) {
        headless = Boolean.TRUE;
    } else {
        headless = Boolean.FALSE;
    }

If you want to know if there is any screen available, you can invoke GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() which returns all the available screens.

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;

public class TestHeadless {

    private static boolean isReallyHeadless() {
        if (GraphicsEnvironment.isHeadless()) {
            return true;
        }
        try {
            GraphicsDevice[] screenDevices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
            return screenDevices == null || screenDevices.length == 0;
        } catch (HeadlessException e) {
            e.printStackTrace();
            return true;
        }
    }

}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • thank you for the answer. My only concern is that, when i invoke the isHeadless() method and i am running on a console unix environment, is it going to return true or false? Because from the JavaDoc i could see that: "Tests whether or not a display, keyboard, and mouse can be supported in this environment ", thus i thought even in unix (keybord will be availble) it will return true. But i think your code will solve my problem. thanks again. – nikkatsa May 17 '13 at 15:09
  • 2
    @nikkatsa it's all explained in my answer above. This information has been directly taken from the analysis of the source code. – Guillaume Polet May 17 '13 at 15:39