12

For a J2EE bean I am reusing code that was developed for a java swing application. JOptionPane.showMessageDialog() is unfortunately commonly used. Most occurences luckily in code sections that are not reused by the J2EE application, but in some cases lower levels of the code has instances of JOptionPane.showMessageDialog(). Obviously this it results in dialog boxes popping up on the server, which is what I want to avoid.

As a first step I'd like to somehow assure that no dialog boxes will ever occur on the server.

Someone suggested peeking in some event or paint queue (I do not recall which one): That would be:

// old code: JOptionPane.showMessageDialog(msg);
if ( someEventQueue.size() == 0 ) // <== consider this pseudo-code
  Log.log(msg); // I am running on a server. Tell the log.
else
  JOptionPane.showMessageDialog(msg); // I have a user made of meat. Tell him!

I never really got that working. What would you do?

Sampada
  • 2,931
  • 7
  • 27
  • 39
fredarin
  • 784
  • 5
  • 14
  • 1
    I would try to do it the correct way. Have an interface with a method `logToUser`, and either pass an instance of it around in your code or tuck it into some static field (if the other is infeasible). Where you log, just call that method. The users of the code on the server will have to provide an implementation, and the Swing application provide another (and an Android or SWT or another application will have to provide yet another). Make sure to check early if the implementation is set and throw an exception if not... – mihi Dec 30 '13 at 15:21

2 Answers2

26

Make sure the server is started with

java -Djava.awt.headless=true

Most servers should be started that way by default. Then you can check:

boolean headless_check = GraphicsEnvironment.isHeadless();

More details on headless available here:

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Brian
  • 13,412
  • 10
  • 56
  • 82
  • Or any custom system parameter, for example -Dnodialog=true . – akarnokd Jun 18 '09 at 19:24
  • Good Point. There is a plethora of System properties that would be unique to each environment. For Oracle OC4J application Server: System.getProperty("oracle.oc4j.instancename"); – Brian Jun 18 '09 at 19:41
  • Its not about system dependant properties. You could use anything you want: -Dthis_is_a_param_for_no_dialog=true – akarnokd Jun 18 '09 at 19:46
  • 1
    The great thing about using the JVM option -Djava.awt.headless=true is that HeadlessException will be thrown each time JOptionPane.showMessageDialog(msg) is executed - and so on. – fredarin Jun 19 '09 at 02:04
  • @Brian The "details about headless" link is gone. Care to provide an updated link ? Thanks – Vincent Fourmond Sep 18 '13 at 19:58
  • @VincentFourmond I updated the link. I believe this is the article I referenced back then: http://www.oracle.com/technetwork/articles/javase/headless-136834.html – Brian Sep 20 '13 at 15:51
  • 4
    BTW: `GraphicsEnvironment.isHeadless()` ... because `isHeadless()` is a static class method. Or use `ge.isHeadlessInstance()` otherwise. ...6 years and no one noticed? O_o – Yanick Rochon May 15 '15 at 03:48
  • @Yanick Rochon: Are you sure `ge.isHeadlessInstance()` can be used instead? Java 8 docs of static `isHeadless()`: "true if this environment cannot support a display, keyboard, and mouse; false otherwise" Docs of `isHeadlessInstance()`: "true if a display, keyboard, and mouse can be supported in this environment; false otherwise" - The exact opposite. Are the docs wrong? – Dreamspace President Apr 13 '16 at 07:32
  • 1
    @DreamspacePresident My point was mainly that this answer calls a static method from an instance object. While Java does support this, `isHeadless()` is actually static... so you really don't need to call `GraphicsEnvironment.getLocalGraphicsEnvironment()` and return an instance to call the method. I actually personally only used this static method when writing a small server. I never had (or saw pertinent) to create an instance and call the `isHeadlessInstance()` method. If not asked before, this can be a good SO question. – Yanick Rochon Apr 13 '16 at 14:11
  • It is worthy of note that, at least on OS X 10.10.5 with Oracle JDK 1.8.0_60, `GraphicsEnvironment.getLocalGraphicsEnvironment()` itself causes GUI elements to appear (icon and top-level menu). `isHeadless()` and `isHeadlessInstance()` both appear to simply return the result of static method `getHeadlessProperty()`. Executing with `-Djava.awt.headless=true` avoids creating any GUI elements. – neuralmer May 23 '16 at 18:24
0

I just tested this with OpenJDK Runtime Environment (IcedTea 2.4.3) (Gentoo build 1.7.0_45-b31) on Linux, and I find that unset DISPLAY was also sufficient to make isHeadless() (and isHeadlessInstance()) return true.

So, this method doesn't just tell you whether AWT was forced to ignore the system's graphics capabilities, but rather whether your process has access to graphics capabilities at all.

Example:
The code is running on some box that has no display card (that's probably what you want to know) so you can always check the system property if for some reason you want to know whether headless mode was forced.

staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
Mark Wood
  • 356
  • 1
  • 9