How to Fix the No X11 Display Error With Java Applications on Ubuntu 24.04 LTS

On my newly upgraded Ubuntu 24.04 LTS I got this error when I started a Java application:

Exception in thread “main” java.awt.HeadlessException:
No X11 DISPLAY variable was set,
or no headful library support was found,
but this program performed an operation which requires it,

at java.desktop/sun.java2d.HeadlessGraphicsEnvironment.getDefaultScreenDevice(HeadlessGraphicsEnvironment.java:58)
at foo.lj.a(Unknown Source)
at foo.lj.(Unknown Source)

This problem is a tricky one, then the error has nothing to do with a missing variable and most results in Google point you in the wrong direction.

The source of this problem is that Ubuntu installs the headless option for the OpenJDK by default. That works great for everything that does not has a GUI. But as soon as you start an application with a graphical user interface, you end up with the error from above.

We can check what version of the OpenJDK we have on our system with this command:

I got an extensive list of packages, but for the newest version of OpenJDK I only got the headless package. To install the full OpenJDK, we can use this command:

After installing this package, my Java application started without any problems.

1 thought on “How to Fix the No X11 Display Error With Java Applications on Ubuntu 24.04 LTS”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.