錯誤: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.html
錯誤詳情:java
[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。linux
分析:shell
1.經過查詢咱們得知,Linux默認狀況下是啓動x11 windows server的,並且它是與windows的圖形工具顯示相關;windows
2.變量DISPLAY的值與程序界面顯示在機器的哪一個圖形化終端有關,經過執行who命令和who am i 命令,能夠知道當前圖形化終端是第n個(以下是第2個),那麼變量DISPLAY的值(值格式是I P:A.B, 其中B默認是0)應該是 IP:(n-1) 或者 :(n-1)api
解決方法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鏈接到-(選中)Xmanagerapp
而後打開xshell會話後:
echo $DISPLAY 查看是有值的
此時直接運行腳本能夠打開程序GUI界面dom
解決方法2:ssh
當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的值不匹配,能夠詢問該機器的管理員查看機器是否正常開啓,或者找其餘圖形終端的訪問者讓其關閉遠程桌面訪問,僅保留你的遠程桌面,而後再嘗試從新執行程序。