4

I am using java 1.7.0_67 on mac osx 10.7.5. Here is my hello world gui:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class helloWorld extends JFrame {
    helloWorld(String title) { 
        this.setSize(500,500); 
        setTitle(title); 
    }

    public static void main(String[] args) {
       helloWorld window = new helloWorld("Helloworld");
       window.setVisible(true);
       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }
}

This runs just fine when i open a mac terminal and type java helloWorld. However, when I ssh into my mac from another host, set my DISPLAY env variable, and run, I get the following exception:

Exception in thread "main" java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
    at java.awt.GraphicsEnvironment.checkedHeadless(GraphicsEnvironment.java:207)
    at java.awt.Window.<init>(Window.java:535)
    at java.awt.Frame.<init>(Frame.java:420)
    at java.awt.Frame.<init>(Frame.java:385)
    at javax.swing.JFrame.<init>(JFrame.java:180)
    at helloWorld.<init>(helloWorld.java:8)
    at helloWorld.main(helloWorld.java:14)

This used to work on Java 1.6, from what I've been able to research this appears to be a purposeful break.

Any ideas how I can get my gui to display after ssh-ing in from a remote host? I don't want to use X11 (would prefer native gui rendering).

gnr
  • 2,324
  • 1
  • 22
  • 24
  • unfortunately that doesn't work. `-Djava.awt.headless=false` also doesn't work for me. from what I understand, this is a mac-specific problem because of how mac osx sets up a terminal started through Terminal vs started through ssh. `ssh -X` doesn't work either – gnr Oct 06 '14 at 18:28
  • use `ssh -X` to enforce X11 protocol tunneling under ssh. – Jean-Baptiste Yunès Oct 06 '14 at 18:31
  • possible duplicate of ["No X11 DISPLAY variable" - what does it mean?](http://stackoverflow.com/questions/662421/no-x11-display-variable-what-does-it-mean) – StackFlowed Oct 06 '14 at 18:32
  • Thanks - I had seen that one, but that's about displaying on a linux box, not on mac os x - for that matter I don't even know why the error mentions X11 since I'm not using X11 and displaying the gui natively – gnr Oct 06 '14 at 19:13
  • @Jean-BaptisteYunès unfortunately this does not work - here i'm using mac os directly to display the gui, not X11 – gnr Oct 06 '14 at 19:14
  • Then you should install X11 for your mac, no way! http://xquartz.macosforge.org/trac – Jean-Baptiste Yunès Oct 07 '14 at 06:48
  • @Jean-BaptisteYunès i do have X11 already on my mac, but prefer the mac's native rendering (esp wrt fonts). thanks though :) – gnr Oct 07 '14 at 10:42

2 Answers2

3

The Java developers chose to use the headless Toolkit when running in an ssh session on Mac OS X. You can convince Java to go ahead and display the GUI anyway by setting the AWT_TOOLKIT environment variable to CToolkit. For example, in bash:

export AWT_TOOLKIT=CToolkit
java helloWorld

With the variable set, your GUI should display as you expect.

gnr
  • 2,324
  • 1
  • 22
  • 24
atomicpirate
  • 657
  • 5
  • 12
  • @duskwuff - this worked for me, curious as to what you think about this solution – gnr Oct 06 '14 at 20:36
  • This even works when I have multiple users logged in to one mac with multiple desktops: seems it chooses by user-id, which makes sense. – android.weasel Sep 28 '15 at 17:15
  • Though curiously, it doesn't work on my MBP - just on the Mac Minis we have as build agents. Perhaps it's because I have (had) a Quartz session locally? I can trivially 'open /Applications/Sikuli.app' and see an icon on the dock, so the login sessions are somehow communicating. – android.weasel Sep 28 '15 at 19:44
  • Java 1.8.0_45 on MBP and 1.7.0_71 on mac minis could also be a factor. – android.weasel Sep 28 '15 at 20:15
  • Yup - Copied 1.7 to my MBP and set JAVA_HOME to it. Combined with AWT_TOOLKIT, it happily started up from ssh on the Mac desktop. – android.weasel Sep 28 '15 at 22:15
  • this did not work for me on ubuntu - however I was running intellij idea fine with X11 forwarding, but when I started my own app in the intellij IDE, then I got following error: Graphics Device initialization failed for : es2, sw Error initializing QuantumRenderer: no suitable pipeline found java.lang.RuntimeException: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found – serup Aug 11 '16 at 10:51
  • This didn't work for me. I get `WindowServer is not available` followed by a stack trace that mentions `LWCToolkit`. – Indiana Kernick May 22 '21 at 06:57
0

You can't. To connect to the window server on Mac OS X, the process must be in the current user's login session. SSH is not part of the login session, so processes run through SSH cannot connect to the window server, and thus cannot display a GUI.

  • really? i can do this on mac osx 10.7.3 and Java 1.6.0_29, did something change between versions? in any case, any way I can fake this (e.g. set environment variables, change some config file etc)? – gnr Oct 06 '14 at 19:27
  • 1
    I don't know why this might have worked on previous versions of Mac OS X. And no, there is no way to override this behavior. –  Oct 06 '14 at 19:38
  • 1
    I can 'open some.jpg' in an ssh shell and it will appear on my desktop, and 'open some.sh' will execute 'some.sh ; exit' in a terminal window with all the desktop env-vars defined, and if I use the -W option it will wait for the application to quit (though annoyingly it won't pass in arguments after --args), so this answer isn't strictly true. (Though I'm running as admin) – android.weasel Sep 28 '15 at 21:11