本文主要是介绍Could not initialize class java.awt.Font,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目场景:
项目场景:java项目在web端导出Excel、Word、PDF等文档
问题描述
在Windows系统中开发以及运行文件导出正常,单机部署到Linux中或者使用docker部署后,导出报错。
异常:
eleasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@66d04f07]
Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@66d04f07]
Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@66d04f07]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@66d04f07]
2024-03-30 06:45:07.954 ERROR 11374 --- [nio-9257-exec-5] .security.handler.GlobalExceptionHandler: 请求地址'/simulator/1/1752881286541737984/air_data/downloadData',发生系统异常.
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class java.awt.Fontat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1082) ~[spring-webmvc-5.3.19.jar!/:5.3.19]at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.19.jar!/:5.3.19]at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.19.jar!/:5.3.19]at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.19.jar!/:5.3.19]at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.62.jar!/:4.0.1]at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.19.jar!/:5.3.19]at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.62.jar!/:4.0.1]at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.62.jar!/:?]at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar!/:?]at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.62.jar!/:?]at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar!/:?]at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar!/:?]at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.19.jar!/:5.3.19]at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.19.jar!/:5.3.19]at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar!/:?]at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar!/:?]at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.19.jar!/:5.3.19]
原因分析:
代码中添加了图形化配置(以下是直接从别的项目中copy过来的)
解决方案:
方案一(适用于项目中已导入使用的java依赖)
删掉以上代码或者将
false
修改为true
例如:新建一个 Message
对象,并将读取到的数据存入 Message
,然后 mHandler.obtainMessage(READ_DATA, bytes, -1, buffer).sendToTarget();
换成 mHandler.sendMessage()
。
方案二(适用于项目本身就没有导入依赖)
jvm无法初始化该类,由于本linux没有安装图形化界面;在web server(tomcat、nginx)中配置一下即可
在tomcat/bin/catalina.sh 中增加JAVA_OPTS=“$JAVA_OPTS -Djava.awt.headless=true” 配置即可
vim catalina.sh
在开头插入如下语句
JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true"
这篇关于Could not initialize class java.awt.Font的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!