2

Update: as marked duplicate, I just want to mention, this seems like a duplicate, but the answer to the other mentioned question is not completely correct. Instead refer to the accepted answer below. isHeadless would return unexpected true in certain cases.

Its a bit weird situation, but recently I build a very simple java application which can be run in console/terminal mode or in JavaFX UI mode.

However, then while using it on a remote computer which doesn't have any display attached. I got an error that this JavaFX UI application can't be initiated on systems without display, which is pretty obvious.

To overcome this problem, I have been looking for a robust way of detecting if the system has any display attached and it can initiate a JavaFX application, which has to be a platform independent solution, since it could be Windows or Ubuntu/Linux or Mac system.

Structure of the application:

A Main console app, which depending on input arguments executes internally a console app or UI app.

So that, if any arguments given, run in console mode or if no arguments then run in UI mode. This is where I want to detect if there is a display available from within my main console app, which then won't even try to run the UI app if display is missing.

Any idea how can we achieve this or suggestion in a proper direction would be great.

Indigo
  • 2,887
  • 11
  • 52
  • 83
  • what about a Try..catch? Can you detect the exact error code/message/type? – jean Feb 23 '15 at 11:02
  • @jean: yes absolutely true, but I was looking for more like an internal Java way of detecting this instead of even going up to the catching exception stage. That's why I said a robust way. Because since Java is throwing this exception that means it must have been reading it or detecting it someway. That is what I want to know :) – Indigo Feb 23 '15 at 11:06

1 Answers1

2

I think you could use java.awt.GraphicsEnvironment

GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();

which will return an array with all the available screens. If this array is empty, there is no monitor.

Edit: About using isHeadless(), you can look at How to determine if GraphicsEnvironment exists

Community
  • 1
  • 1
antonio
  • 18,044
  • 4
  • 45
  • 61
  • Yes exactly this is what I wanted. But just looking at the question marked duplicate by Vulcan [here](http://stackoverflow.com/a/4245227/1584507), and that seems like a proper approach, any comments on that? – Indigo Feb 23 '15 at 11:49
  • 1
    Edited my answer with a link about GraphicsEnvironment.isHeadless() – antonio Feb 23 '15 at 11:54
  • Thanks, that cleared all the smoke :) – Indigo Feb 23 '15 at 12:56