远程启动的服务依赖显示环境导致java.awt.HeadlessException

BraveWangDev 2012-10-23

服务器没有显示器,通过ssh登录启动的基于java的web服务,在java中有段生成缩略图的代码调用了Toolkit.getDefaultToolkit().getScreenResolution();,依赖显示环境

以下记录几个实验结果:

1、ssh user@ip之后,直接启动jetty.sh start,在生成缩略图时报错

java.awt.HeadlessException: 
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
	at sun.awt.HeadlessToolkit.getScreenResolution(HeadlessToolkit.java:221)

     这个解决办法:在ssh登录时加上 -X 选项

2、ssh -X user@ip 之后,设置-Djava.awt.headless=true,再启动jetty.sh start &,在生成缩略图时报错:

java.awt.HeadlessException
        at sun.awt.HeadlessToolkit.getScreenResolution(HeadlessToolkit.java:221)
 

3、ssh -X user@ip 之后,再启动jetty.sh start,没有问题

4、ssh -X user@ip 之后,设置-Djava.awt.headless=false,再启动jetty.sh start &,在生成缩略图时报错:

java.lang.InternalError: Can't connect to X11 window server using 'localhost:11.0' as the value of the DISPLAY variable.
        at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
        at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:62)
        at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:178)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:142)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:186)
        at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
        at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:112)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:186)
        at java.awt.Toolkit$2.run(Toolkit.java:849)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:841)

相关推荐