linux 系统上图形生成错误 java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment

错误信息:

02-Jun-2024 09:11:09.421 SEVERE [Thread-32] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [springDispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment] with root cause
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment

原因分析:

验证码采用awt图形处理并内存中生成,java程序会去寻找linux上的图形界面是否启动 ,如linux缺少组件则, 缺少了显示设备、键盘或鼠标会报这个错误;

所以需要开启无头模式, Headless模式是系统的一种配置模式 , 开始激活headless模式,告诉程序,现在你要工作在Headless mode下,就不要指望硬件帮忙了,你得自力更生,依靠系统的计算能力模拟出这些特性来继续;

解决方案:

jar启动时,增加如下命令:

-Djava.awt.headless=true
war包启动,更改tomcat/bin/catalina.sh的文件,增加:

JAVA_OPTS="$JAVA_OPTS -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Djava.awt.headless=true"

 

posted @ 2024-06-02 09:23  李小加  阅读(49)  评论(0编辑  收藏  举报