How to run a java GUI on mac osx after ssh

风流意气都作罢 提交于 2019-12-11 03:26:29

问题


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).


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/26222355/how-to-run-a-java-gui-on-mac-osx-after-ssh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!