I need my Java program to have two display modes: a GUI interface and a command line interface. If I run it in Windows, OS X, or another graphical environment I should get the GUI interface, but if I run it via SSH I should get the command line interface.
How can I detect whether a GUI can be displayed or if I should use a command line interface?
But there is a major difference between CLI and GUI. GUI lets a user interact with the device/system with the help of graphical elements, like windows, menus, icons, etc. The CLI, on the other hand, lets a user interact with their device/system with the help of various commands.
CLI is faster than GUI. The speed of GUI is slower than CLI. 5. CLI operating system needs only a keyboard.
Note: The isHeadless() method checks the specific system property, java. awt. headless , instead of the system's hardware configuration. A HeadlessException is thrown when code that depends on a display device, keyboard, or mouse is called in an environment that does not support any of these.
You actually have two questions:
1) Check if you run in a headless environment (no graphics). Check this method:
if (GraphicsEnvironment.isHeadless()) { // non gui mode } else { // gui mode }
2) Check which OS you are running under:
System.getProperty("os.name")
However, the second (2) question will return the same name even though you operate in a headless environment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With