title: How to run a Java Swing application from Linux shell without a desktop environment?
body: <p>I have an example Java application for testing. It uses the Java Swing library. I try running it with <code>xserver</code> in the Linux shell, so I'm not using a desktop environment.</p>
  
  <p>Usually, you can run e.g. Firefox without problems like this:</p>
  
  <pre><code>startx /usr/bin/firefox    
  </code></pre>
  
  <p>I tried exporting <code>DISPLAY</code> and running the application headless, but it only printed the first of the following lines.</p>
  
  <pre class="lang-java prettyprint-override"><code>{
      System.out.println("This works");
      JOptionPane.showMessageDialog(null, "This should show up");
  }
  </code></pre>
  
  <p>If I run <code>java -jar app.jar</code> from the command line, I get a following error message:</p>
  
  <pre><code>Exception in thread "main" java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it.
  </code></pre>
  
  <p>With <code>startx java -jar app.jar</code>, the output is:</p>
  
  <pre><code>Bad command line option "-jar"
  </code></pre>