错误:Xshell远程连接或者本地Linux OS下启动java程序报错:
No X11 DISPLAY variable was set,
或者 Can't connect to X11 window server using ':1.0' as the value of the DISPLAY variable.
错误详情:
[root@neokylin import_jre]# ./run_linux64
/root/lyy/import_jre
/root/lyy/import_jre/lib
/root/lyy/import_jre/jre8_openjdk64/bin
classpath is: /root/lyy/import_jre/lib/myapp.jar:/root/lyy/import_jre/jre8_openjdk64/lib/ext:/root/lyy/import_jre/lib/lib/commons-io-2.0.1.jar:/root/lyy/import_jre/lib/lib/dom4j-1.6.1.jar:/root/lyy/import_jre/lib/lib/hgdb-1.3-jdbc41.jar:/root/lyy/import_jre/lib/lib/log4j-1.2.17.jar:/root/lyy/import_jre/lib/lib/poi4excel.jar:/root/lyy/import_jre/lib/lib/slf4j-api-1.7.2.jar:/root/lyy/import_jre/lib/lib/slf4j-log4j12-1.7.2.jar
run now...
No protocol specified
Exception in thread "main" java.awt.AWTError: Can't connect to X11 window server using ':1.0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:66)
at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:187)
at java.security.AccessController.doPrivileged(Native Method)
at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:146)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:102)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:81)
at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:123)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at java.awt.Toolkit$2.run(Toolkit.java:860)
at java.awt.Toolkit$2.run(Toolkit.java:855)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:854)
at javax.swing.UIManager.getSystemLookAndFeelClassName(UIManager.java:611)
at hgdbmonitor.view.MonitorView.main(MonitorView.java:2147)
错误原因:找错误时应该去看错误提示Can't connect to X11 window server using ':1.0' as the value of the DISPLAY variable.从中我们找到了两个关键点:x11 windows server和变量DISPLAY。
分析:
1.通过查询我们得知,Linux默认情况下是启动x11 windows server的,而且它是与windows的图形工具显示相关;
2.变量DISPLAY的值与程序界面显示在机器的哪个图形化终端有关,通过执行who命令和who am i 命令,可以知道当前图形化终端是第n个(如下是第2个),那么变量DISPLAY的值(值格式是I P:A.B, 其中B默认是0)应该是 IP:(n-1) 或者 :(n-1)
解决方法1:
首先确保服务器的ssh设置中配置为 X11Forwarding yes
参考:Xshell: https://www.netsarang.com/tutorial/xshell/1018/Using_X11_forwarding
vi /etc/ssh/sshd_config
配置:X11Forwarding yes
然后重启服务service sshd restart
然后确保xshell客户端配置为:
属性-连接-SSH-隧道:
X11转移-(选中)转发X11连接到-(选中)Xmanager
然后打开xshell会话后:
echo $DISPLAY 查看是有值的
此时直接运行脚本可以打开程序GUI界面
解决方法2:
当DISPLAY环境变量未自动生成时,手动设置:
本地设置:
export DISPLAY=:0.0
远程设置:
who
who am i
根据结果确定x的具体数值
export DISPLAY=localhost:x.0
注意:变量DISPLAY的值与程序界面显示在机器的哪个图形化终端有关,通过执行who命令和who am i 命令,可以知道当前图形化终端是第n个(如下是第2个),那么变量DISPLAY的值(IP:A.B, 其中B默认是0)应该是IP:(n-1)或者:(n-1)(如下是:1.0),只有这样java图形程序的界面才能正常显示出来。
[root@neokylin import_jre]# who
root tty2 2015-02-06 13:18
root pts/1 2015-02-06 13:42(192.168.100.126)
highgo pts/2 2015-02-06 13:42(:1.0)
[root@neokylin import_jre]# who am i
highgo pts/2 2015-02-06 13:42(:1.0)
[root@neokylin import_jre]# echo $DISPLAY
:1.0
如果DISPLAY的值不匹配,可以询问该机器的管理员查看机器是否正常开启,或者找其他图形终端的访问者让其关闭远程桌面访问,仅保留你的远程桌面,然后再尝试重新执行程序。
参考资料:http://www.itpub.net/thread-1633000-1-1.html
来源:oschina
链接:https://my.oschina.net/u/1158288/blog/376091