报错解决:Exception sending context initialized event to listener instance of class org.apache.myfaces.webapp.StartupServletContextListener: java.lang.InternalError: Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable.
一、现象
今天给客户做的系统在应用重启的时候应用启动日志碰到了下述报错:
JBWEB000287: Exception sending context initialized event to listener instance of class org.apache.myfaces.webapp.StartupServletContextListener: java.lang.InternalError: Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method) [rt.jar:1.6.0_37]
at sun.awt.X11GraphicsEnvironment.access$100(X11GraphicsEnvironment.java:52) [rt.jar:1.6.0_37]
at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:155) [rt.jar:1.6.0_37]
at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.6.0_37]
at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:131) [rt.jar:1.6.0_37]
at java.lang.Class.forName0(Native Method) [rt.jar:1.6.0_37]
at java.lang.Class.forName(Class.java:169) [rt.jar:1.6.0_37]
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68) [rt.jar:1.6.0_37]
at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:89) [rt.jar:1.6.0_37]
at java.lang.Class.forName0(Native Method) [rt.jar:1.6.0_37]
at java.lang.Class.forName(Class.java:169) [rt.jar:1.6.0_37]
at java.awt.Toolkit$2.run(Toolkit.java:834) [rt.jar:1.6.0_37]
at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.6.0_37]
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:826) [rt.jar:1.6.0_37]
at org.richfaces.application.InitializationListener$AWTInitializer.initialize(InitializationListener.java:192) [richfaces-core-impl-4.3.5.Final.jar:4.3.5.Final]
at org.richfaces.application.InitializationListener.initializeAWT(InitializationListener.java:94) [richfaces-core-impl-4.3.5.Final.jar:4.3.5.Final]
at org.richfaces.application.InitializationListener.onStart(InitializationListener.java:78) [richfaces-core-impl-4.3.5.Final.jar:4.3.5.Final]
at org.richfaces.application.InitializationListener.processEvent(InitializationListener.java:152) [richfaces-core-impl-4.3.5.Final.jar:4.3.5.Final]
at javax.faces.event.SystemEvent.processListener(SystemEvent.java:43) [myfaces-api-2.1.12.jar:2.1.12]
at org.apache.myfaces.application.ApplicationImpl._traverseListenerList(ApplicationImpl.java:2471) [myfaces-impl-2.1.12.jar:2.1.12]
at org.apache.myfaces.application.ApplicationImpl.access$000(ApplicationImpl.java:120) [myfaces-impl-2.1.12.jar:2.1.12]
at org.apache.myfaces.application.ApplicationImpl$SystemListenerEntry.publish(ApplicationImpl.java:2662) [myfaces-impl-2.1.12.jar:2.1.12]
at org.apache.myfaces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:601) [myfaces-impl-2.1.12.jar:2.1.12]
at org.jboss.as.weld.webtier.jsf.ForwardingApplication.publishEvent(ForwardingApplication.java:288) [jboss-as-weld-7.2.0.Final.jar:7.2.0.Final]
at org.apache.myfaces.webapp.AbstractFacesInitializer._dispatchApplicationEvent(AbstractFacesInitializer.java:286) [myfaces-impl-2.1.12.jar:2.1.12]
at org.apache.myfaces.webapp.AbstractFacesInitializer.initFaces(AbstractFacesInitializer.java:154) [myfaces-impl-2.1.12.jar:2.1.12]
at org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized(StartupServletContextListener.java:119) [myfaces-impl-2.1.12.jar:2.1.12]
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3339) [jbossweb-7.4.0.Final.jar:7.4.0.Final]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3777) [jbossweb-7.4.0.Final.jar:7.4.0.Final]
at org.jboss.as.web.deployment.WebDeploymentService.doStart(WebDeploymentService.java:156) [jboss-as-web-7.2.0.Final.jar:7.2.0.Final]
at org.jboss.as.web.deployment.WebDeploymentService.access$000(WebDeploymentService.java:60) [jboss-as-web-7.2.0.Final.jar:7.2.0.Final]
at org.jboss.as.web.deployment.WebDeploymentService$1.run(WebDeploymentService.java:93) [jboss-as-web-7.2.0.Final.jar:7.2.0.Final]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439) [rt.jar:1.6.0_37]
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) [rt.jar:1.6.0_37]
at java.util.concurrent.FutureTask.run(FutureTask.java:138) [rt.jar:1.6.0_37]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_37]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_37]
at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_37]
at org.jboss.threads.JBossThread.run(JBossThread.java:122)
二、分析排查
就改了个应用配置文件,其他啥也没改,然后重启应用就报了这个错。中间差点使用这个stackoverflow的帖子
Java Can’t connect to X11 window server using ‘localhost:10.0’ as the value of the DISPLAY variable
里边的方法跑export DISPLAY=:0
或unset DISPLAY
这两个命令尝试。但是直觉告诉我在生产服务器跑这两个命令有风险!所以我在没到不得已都没告诉客户跑这个命令。
身边有另外两位同事也在想原因,其中一位说“会不会是不小心误操作搞坏了什么环境变量了”,然后启发了我,我一想“会不会和他们传文件的方式有问题”,自然又想到“用了啥图形化软件传进来,自动把DISPLAY参数改了”,于是一问,果然是大坑的 xshell !之前碰过一会在客户测试环境交付的时候,在他们办公台式机用 xshell 命令行,结果当关闭 xshell 的时候,期间打开的所有进程(无论是 java进程还是连 tail -f 进程)都一概被 kill 掉了。可见 XSHELL 对服务器有很强的侵入式。
于是让客户换了个命令行软件(SecureCRT),然后再重启应用,问题解决,启动没任何问题。
后来咨询了其他团队的同事,也碰到过用 xshell 会开启图形化的参数,导致报上述 swing 和 X11 window 的错误,进而导致应用起不来,解决办法是修改 xshell 选项里边的图形化参数。
三、解决办法:
1、弃用 xshell 改用其他诸如 putty 或 SecureCRT 等命令行终端软件,然后重启应用就搞定了。
2、设置 xshell 选项中的图形化参数,具体我也没试,所以这里不放图,毕竟前面踩过 xshell 杀进程的坑之后这次不敢再用它了。
3、慎用 export DISPLAY=:0
或unset DISPLAY
这两个命令,我没尝试过是不是能解决,但是这种改生产服务器系统变量的命令,千万慎用;而且问题本身出在终端软件,和这个环境变量无关。