In my case it was not a problem with the display server or a window manager on my Arch Linux. Other GUI apps launched without a hitch from terminal. After a little digging I decided to print out all the system properties (System.getProperties()
) and realized that the JVM (java
executable) I was using was not from the JDK downloaded from Oracle's website but from the one supplied by my system's package manager.
Apparently, my system had headless version of openjdk installed. The JVM supplied by this openjdk package had no support for display and peripheral devices, hence headless. That's why I was getting the HeadlessException.
Some folks suggest to install the regular version of openjdk where JVM runs in non-headless mode unless instructed to do otherwise. Other solution is to always use JVM from the JDK you downloaded from Oracle. I chose the latter.
I setup an alias in my bash to java keyword.
alias java="/path/to/downloaded/jdk/bin/java "
I then tested the graphics capabilities of both JVMs with this code:
import java.awt.GraphicsEnvironment;
// Inside main method:
System.out.println(GraphicsEnvironment.isHeadless());
The above code printed true when executed with system's JVM and printed false when executed with downloaded JVM. My actual code using Swing ran fine too.
DISPLAY
environment variable to the address of your X11 server, e.g.DISPLAY=localhost:0.0
, before starting your Java application. – Montane