Linux下java.awt.HeadlessException的解决

时间:2021-07-21 02:18:12

最近做的一个Java Web项目采用Struts2+Spring+Hibernate的架构,开发环境是win7,服务器部署环境是Linux,项目中有一个模块是采用JFreeChart生成图片,再创建并导入excel文件中,开发完成后在windows下的tomcat7.0本地环境没问题,但是部署到Linux服务器上的tomcat7.0之后就有页面报错如下:

HTTP Status 500 -


type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.RuntimeException: baseSrc.framework.BaseException: java.lang.reflect.InvocationTargetException
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
com.glaf.base.servlet.StrutsActionServlet.process(StrutsActionServlet.java:60)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
com.glaf.base.filter.ForbiddenCacheFilter.doFilter(ForbiddenCacheFilter.java:26)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

root cause

java.lang.RuntimeException: baseSrc.framework.BaseException: java.lang.reflect.InvocationTargetException
baseSrc.framework.BaseAction.execute(BaseAction.java:157)
org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:113)
org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
com.glaf.base.servlet.StrutsActionServlet.process(StrutsActionServlet.java:60)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
com.glaf.base.filter.ForbiddenCacheFilter.doFilter(ForbiddenCacheFilter.java:26)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

root cause

baseSrc.framework.BaseException: java.lang.reflect.InvocationTargetException
baseSrc.framework.BaseAction.exeDaoMethod(BaseAction.java:286)
baseSrc.framework.BaseAction.execute(BaseAction.java:152)
org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:113)
org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
com.glaf.base.servlet.StrutsActionServlet.process(StrutsActionServlet.java:60)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
com.glaf.base.filter.ForbiddenCacheFilter.doFilter(ForbiddenCacheFilter.java:26)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

root cause

java.lang.reflect.InvocationTargetException
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
baseSrc.framework.BaseAction.exeDaoMethod(BaseAction.java:261)
baseSrc.framework.BaseAction.execute(BaseAction.java:152)
org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:113)
org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
com.glaf.base.servlet.StrutsActionServlet.process(StrutsActionServlet.java:60)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
com.glaf.base.filter.ForbiddenCacheFilter.doFilter(ForbiddenCacheFilter.java:26)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

root cause

java.awt.HeadlessException
java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
java.awt.Window.<init>(Window.java:433)
java.awt.Frame.<init>(Frame.java:403)
javax.swing.JFrame.<init>(JFrame.java:202)
org.jfree.chart.ChartFrame.<init>(ChartFrame.java:76)
org.jfree.chart.ChartFrame.<init>(ChartFrame.java:64)

net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
mqis.src.ppm.DAO.Page4208DAO$$EnhancerByCGLIB$$6733db05.runPageLoad(<generated>)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
baseSrc.framework.BaseAction.exeDaoMethod(BaseAction.java:261)
baseSrc.framework.BaseAction.execute(BaseAction.java:152)
org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:113)
org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
com.glaf.base.servlet.StrutsActionServlet.process(StrutsActionServlet.java:60)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
com.glaf.base.filter.ForbiddenCacheFilter.doFilter(ForbiddenCacheFilter.java:26)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.12 logs.


怀疑是服务器环境没有装 X11,经过排查后服务器已经安装有了。

借鉴网友的做法,在tomcat服务器上修改${TOMCAT_HOME}/bin/catalina.sh或${TOMCAT_HOME}/bin/catalina.bat文件在配置文件末尾加上(或者去除)  -Djava.awt.headless=true(or false) \ 后,发现还是不行。

于是领导提出的建议

java.awt.HeadlessException 
No X11 DISPLAY variable was set, but this program performed an operation which requires it. 
转自:http://www.iteye.com/topic/386716 

在linux环境下需要配置x windons 
执行以下命令: 
1)   startx & 
2)   export DISPLAY=:0.0 
3)   重起tomcat 

注意:,-Djava.awt.headless=true这个参数就不能加在java启动项里 

设置-Djava.awt.headless=false;

最后用VNC4连接到Linux服务器使用指令重启tomcat服务,页面显示图片正常,感觉很奇怪,用之前的SSH连接服务器进行tomcat的重启,还是那个报错。遇到这种情况,需要在Linux服务器使用指令重启服务,原因暂时还不明确。