views:

192

answers:

4

Is it possible to use java gui frameworks (such as Swing, SWT or javaFX) without desktop environment, such as Gnome?

+2  A: 

Although I haven't encountered this situation myself, I would suspect that this would be the case where the HeadlessException would come into play.

The Javadoc for HeadlessException says the following:

Thrown when code that is dependent on a keyboard, display, or mouse is called in an environment that does not support a keyboard, display, or mouse.

The HeadlessException is thrown by the constructors of various classes that deal with the GUI, such as Dialog and JFrame, so I would suspect that in non-GUI environments, the HeadlessException will be thrown when attempting to use a GUI toolkit.

coobird
I think that the OP would be more interested in the system property that ignores HeadlessException ...
kdgregory
+2  A: 

If you're looking to do testing, or need to use some of the image manipulation classes on a server, then a virtual framebuffer will work. I'll assume you're running Linux; I've had good luck with Xvfb.

kdgregory
I've used Xvfb for some AWT based image manipulation on a server in the past and it worked out fine for that.
glenatron
A: 

Basically, no.

The GUI stuff expects something to actually DO the things requested by the GUI code, but the headless property allows much code to run without. Note: This is only for Unixoid JVM's - the Windows one can always sneak into the GUI which is always there.

The typical way to circumvent this is to have a VNC Xserver running or similar, since that provides the necessary functionality without requering graphics hardware for the X server to cuddle.

The crucial question now is, what do you want to achieve?

Thorbjørn Ravn Andersen
+1  A: 

Setting -Djava.awt.headless=true or System.setProperty("java.awt.headless","true") allows using graphics with some limitations. Drawing into an offscreen buffer works well, as discussed here in the context of JFreeChart running on a web server.

trashgod