Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a Java Swing application from Linux shell without a desktop environment?

I have an example Java application for testing. It uses the Java Swing library. I try running it with xserver in the Linux shell, so I'm not using a desktop environment.

Usually, you can run e.g. Firefox without problems like this:

startx /usr/bin/firefox    

I tried exporting DISPLAY and running the application headless, but it only printed the first of the following lines.

{
    System.out.println("This works");
    JOptionPane.showMessageDialog(null, "This should show up");
}

If I run java -jar app.jar from the command line, I get a following error message:

Exception in thread "main" java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it.

With startx java -jar app.jar, the output is:

Bad command line option "-jar"

1 Answers

This should be:

startx /usr/bin/java -jar app.jar —-

Refer to https://askubuntu.com/questions/697978/how-to-pass-a-program-and-its-arguments-to-start or the startx manual page.

Edit: updated based on @chief654’s response.

like image 190
Michiel Leegwater Avatar answered Feb 27 '26 09:02

Michiel Leegwater



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!