26

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?

FThompson
  • 28,352
  • 13
  • 60
  • 93
Filipe YaBa Polido
  • 1,656
  • 1
  • 17
  • 39

5 Answers5

37

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.

dacwe
  • 43,066
  • 12
  • 116
  • 140
  • @dacwe It seemed that the part about OS in the question was just an explanation of what research had been done, rather than a second question. As such, I edited out of the original question, as it was noise. I believe you should edit your answer to reflect the clarified question. – FThompson Feb 23 '15 at 11:28
  • 1
    This is not an entirely correct answer, please refer to this [question here](http://stackoverflow.com/a/16611566/1584507) **isHeadless** would return unexpected true in certain cases and that is not what you want, when you are looking for if there is really a screen or display available. – Indigo Feb 23 '15 at 13:12
  • is there a way I can check this property from command line? – Sanket Patel Jul 17 '20 at 06:28
4

Both dacwe and vitaut are right. I just wanted to add one recommendation. You should use MVC (model-view-controller) pattern when you are designing your application. So, if it is running in UI mode it uses UIView, otherwise the ConsoleView.

Dacwe recommended you how can you decide automatically which mode to use. Due to Java is cross platform language I think the name is operating system is irrelevant for you. The fact that the system has graphic environment is more relevant, so use GraphicsEnvironment.isHeadless().

System.console() will help you to create shell controlled application.

Stephan
  • 41,764
  • 65
  • 238
  • 329
AlexR
  • 114,158
  • 16
  • 130
  • 208
2

I recommend adding a command line option that determines whether to use command-line or graphical user interface, because the user may want to use the command-line interface even in the environment where GUI is available.

You can also check if console device is available or not with System.console().

vitaut
  • 49,672
  • 25
  • 199
  • 336
2

Why not create two classes, one for commandline and one for GUI? Further down you may want to create two products and then you can deliver the headless one without dependencies to graphic libs.

Fredrik
  • 10,626
  • 6
  • 45
  • 81
-4

I was looking for te same solution and came up with simply use an argument so when i run my program from comandline the args differs in length

if(args.length > 0) {
    System.out.println("command line mode");
}
muni
  • 119
  • 13
  • 3
    You can specify arguments for windowed application, too, btw. And, also, you can invoke your application with no arguments from the console, too. – Yanick Rochon May 15 '15 at 03:51