linux 下servlet画图不显示问题
画图代码在windowxp和有些linux服务器上会没有问题,今天在别的机器上发现运行后卡死在Graphics2D g = buffImg.createGraphics();
由于使用了框架,但是没有看到报错。在网上找了解决方法,同时发现网上有些写法差不多但是可以捕获异常。
解决方法:
在tomcat部署目录下的bin目录里面的catalina.sh启动文件加入下面的红色字体部分就可以了(注意该文件的编码为unix)。
elif [ "$1" = "start" ] ;then
shift
touch “$CATALINA_BASE”/logs/catalina.out
if [ "$1" = "-security" ] ; then
echo “Using Security Manager”
shift
“$_RUNJAVA” $JAVA_OPTS $CATALINA_OPTS
-Djava.endorsed.dirs=”$JAVA_ENDORSED_DIRS” -classpath “$CLASSPATH”
-Djava.security.manager
-Djava.security.policy==”$CATALINA_BASE”/conf/catalina.policy
-Dcatalina.base=”$CATALINA_BASE”
-Dcatalina.home=”$CATALINA_HOME”
-Djava.io.tmpdir=”$CATALINA_TMPDIR”
-Djava.awt.headless=true
org.apache.catalina.startup.Bootstrap “$@” start
>> “$CATALINA_BASE”/logs/catalina.out 2>&1 &
if [ ! -z "$CATALINA_PID" ]; then
echo $! > $CATALINA_PID
fi
else
“$_RUNJAVA” $JAVA_OPTS $CATALINA_OPTS
-Djava.endorsed.dirs=”$JAVA_ENDORSED_DIRS” -classpath “$CLASSPATH”
-Dcatalina.base=”$CATALINA_BASE”
-Dcatalina.home=”$CATALINA_HOME”
-Djava.io.tmpdir=”$CATALINA_TMPDIR”
-Djava.awt.headless=true
org.apache.catalina.startup.Bootstrap “$@” start
>> “$CATALINA_BASE”/logs/catalina.out 2>&1 &
if [ ! -z "$CATALINA_PID" ]; then
echo $! > $CATALINA_PID
fi
fi
编辑完成后,重新启动tomcat,然后再运行画图程序,That’s OK!
经过测试,在weblogic中也可以采用类似的方法,然后画图的时候也不需要Xserver。具体的做法就是在weblogic的启动脚本startWebLogic.sh的最后部分中加入“-Djava.awt.headless=true”。如下橙色字体所示(只摘抄部分):
….
${JAVA_HOME}/bin/java ${JAVA_VM} ${MEM_ARGS} ${JAVA_OPTIONS} -Dweblogic.Name=${SERVER_NAME} -Dweblogic.ProductionModeEnabled=${PRODUCTION_MODE} -Djava.security.policy=”${WL_HOME}/server/lib/weblogic.policy”-Djava.awt.headless=trueweblogic.Server
异常如下:
一、
- 500ServletException
- java.lang.NoClassDefFoundError
- atjava.lang.Class.forName0(NativeMethod)
- atjava.lang.Class.forName(Class.java:120)
- atjava.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:58)
- atjava.awt.image.BufferedImage.createGraphics(BufferedImage.java:1011)
- atjava.awt.image.BufferedImage.getGraphics(BufferedImage.java:1001)
- atimage.mvc.vc.VC.Paint(VC.java:66)
- atimage.run.Servlet.doGet(Servlet.java:183)
- atjavax.servlet.http.HttpServlet.service(HttpServlet.java:740)
- atjavax.servlet.http.HttpServlet.service(HttpServlet.java:853)
- atcom.caucho.server.http.FilterChainServlet.doFilter(FilterChainServlet.java:96)
- atcom.caucho.server.http.Invocation.service(Invocation.java:315)
- atcom.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
- atcom.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:346)
- atcom.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:274)
- atcom.caucho.server.TcpConnection.run(TcpConnection.java:139)
- atjava.lang.Thread.run(Thread.java:484)
[ERROR]2005-07-2121:16:28org.jboss.web.localhost.Engine-StandardWrapperValve[jsp]:Servlet.service()forservletjspthrewexception
java.lang.InternalError:Can'tconnecttoX11windowserverusing':0.0'asthevalueoftheDISPLAYvariable.
atsun.awt.X11GraphicsEnvironment.initDisplay(NativeMethod)
atsun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:134)
atjava.lang.Class.forName0(NativeMethod)
atjava.lang.Class.forName(Class.java:141)
atjava.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:62)
atjava.awt.image.BufferedImage.createGraphics(BufferedImage.java:1041)
atjava.awt.image.BufferedImage.getGraphics(BufferedImage.java:1031)
atcom.sww.helper.Image.creatImage(Image.java:30)
三、
SEVERE: Servlet.service() for servlet validatecode threw exception
java.lang.InternalError: Can't connect to X11 window server using '192.168.1.1:1.0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.access$100(X11GraphicsEnvironment.java:52)
at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:155)
at java.security.AccessController.doPrivileged(Native Method)
at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:131)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1135)
at com.newyulong.iptv.webapp.servlet.ValidateCodeServlet.service(ValidateCodeServlet.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
Sep 8, 2013 6:54:02 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet validatecode threw exception
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1135)
at com.newyulong.iptv.webapp.servlet.ValidateCodeServlet.service(ValidateCodeServlet.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
Sep 8, 2013 6:54:02 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet validatecode threw exception
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1135)
at com.newyulong.iptv.webapp.servlet.ValidateCodeServlet.service(ValidateCodeServlet.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
四、
servlet画图类部分代码:
public class ValidateCodeServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
int w = 60;
int h = 20;
int fh = 10;
int cy = 0;
int x = 1;
int cc = 4;
char[] cs = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q'
,'R','S','T','U','V','W','X','Y','Z', '2', '3', '4', '5', '6',
'7', '8', '9'
};
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException {
x = w / (cc + 1);
fh = h - 2;
cy = h - 4;
// image buffer
BufferedImage buffImg = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
Random random = new Random();
// fill image with white color
g.setColor(Color.WHITE);
g.fillRect(0, 0, w, h);
// create font
Font font = new Font("Fixedsys", Font.PLAIN | Font.BOLD, fh);
// set font
g.setFont(font);
g.setColor(Color.blue);
g.drawRect(0, 0, w - 1, h - 1);
..................................................
}