前端验证码图片无法显示,后端提示异常信息:"sun.awt.FontConfiguration.head" is null

前端验证码图片无法显示,后端提示异常信息:"sun.awt.FontConfiguration.head" is null
Spring Boot项目部署到Centos7时正常,但是访问前端页面时图片验证码无法正常显示,并且后端报异常:java.lang.NullPointerException: Cannot load from short array because "sun.awt.FontConfiguration.head" is null

异常信息如下:

2022-12-21 03:55:22.682 ERROR 2136 --- [nio-8088-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.InternalError: java.lang.reflect.InvocationTargetException] with root cause

java.lang.NullPointerException: Cannot load from short array because "sun.awt.FontConfiguration.head" is null
    at java.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1262) ~[na:na]
    at java.desktop/sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:224) ~[na:na]
    at java.desktop/sun.awt.FontConfiguration.init(FontConfiguration.java:106) ~[na:na]
    at java.desktop/sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:706) ~[na:na]
    at java.desktop/sun.font.SunFontManager$2.run(SunFontManager.java:358) ~[na:na]
    at java.desktop/sun.font.SunFontManager$2.run(SunFontManager.java:315) ~[na:na]
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:318) ~[na:na]

系统环境Centos7JDK17

原因分析:根据异常信息可以看出是系统中缺少相应的字体库。

解决办法:安装fontconfig库即可解决,执行如下命令:

yum install -y fontconfig
fc-cache --force

然后重启服务,验证码就可以正常显示了。

the end

热门文章