Spring Boot项目在获取图片验证码时报错sun.awt.FontConfiguration.head及解决办法

Spring Boot项目在获取图片验证码时报错sun.awt.FontConfiguration.head及解决办法
在阿里云Alibaba Cloud Linux 3服务器上运行的Spring Boot项目,项目能正常启动,但是当访问含有图片验证码的页面时总是报错:java.lang.NullPointerException: Cannot load from short array because "sun.awt.FontConfiguration.head" is null,而且图片验证码无法显示。

错误信息:

2021-12-06 17:09:46.137 ERROR 44175 --- [nio-8088-exec-1] 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

Caused by: 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]
    at java.desktop/sun.font.SunFontManager.<init>(SunFontManager.java:315) ~[na:na]
    at java.desktop/sun.awt.FcFontManager.<init>(FcFontManager.java:35) ~[na:na]
    at java.desktop/sun.awt.X11FontManager.<init>(X11FontManager.java:56) ~[na:na]
    ... 65 common frames omitted

原因:系统缺少相应的字体库,我们只需要使用如下命令安装字体库即可得到解决。

yum install fontconfig
fc-cache --force

安装完后在/usr/shared目录就可以看到fontsfontconfig两个目录。

然后重启项目,验证码能正常获取了并且后端也不再报错了。

the end

热门文章