环境

  • 服务器:Linux CentOS 7 64位
  • Tomcat 8.5

错误描述:

Caused by: java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
    at java.lang.Class.forName0(Native Method) ~[?:1.8.0_211]
    at java.lang.Class.forName(Class.java:264) ~[?:1.8.0_211]
    at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:103) ~[?:1.8.0_211]
    at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82) ~[?:1.8.0_211]
    at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1181) ~[?:1.8.0_211]
    at com.idmm.base.util.VailidateCodeUtils.generateImage(VailidateCodeUtils.java:126) ~[classes/:?]
    at com.idmm.sys.action.ValidateCodeAction.refreshCode(ValidateCodeAction.java:90) ~[classes/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_211]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_211]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_211]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_211]
    at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:899) ~[ognl-3.1.15.jar:?]
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1544) ~[ognl-3.1.15.jar:?]
    ... 105 more

        描述:在Linux服务器上执行如下代码的时候,无法获取到对象

Graphics2D g = buffImg.createGraphics();

解决办法:

        问题原因与服务器的环境以及Java版本有关,需要开启java.awt.headless。

        在环境变量文件 /etc/profile 目录下添加:export JAVA_OPTS=-Djava.awt.headless=true

        然后重新加载环境变量文件并重启Tomcat。 

Logo

更多推荐