原创

net.sf.jasperreports.engine.JRRuntimeException: Error initializing graphic environment异常

1.异常由来:
程序在使用JR在做预览PDF的时候,在开发环境测试没有问题。然后部署到腾讯云的正式线上环境,点击预览PDF时,报错。异常如下
只在点击第一次的出现,

2019-08-01 15:46:18 [http-nio-10520-exec-1] ERROR cn.lancelot.framework.web.handler.ExceptionHandel 系统异常[1564645577713]
net.sf.jasperreports.engine.JRRuntimeException: Error initializing graphic environment.
        at net.sf.jasperreports.engine.util.JRGraphEnvInitializer.initializeGraphEnv(JRGraphEnvInitializer.java:63) ~[jasperreports-6.0.0.jar!/:6.0.0]
        at net.sf.jasperreports.engine.fill.BaseReportFiller.<init>(BaseReportFiller.java:122) ~[jasperreports-6.0.0.jar!/:6.0.0]
        at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:229) ~[jasperreports-6.0.0.jar!/:6.0.0]
        at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:74) ~[jasperreports-6.0.0.jar!/:6.0.0]
        at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:62) ~[jasperreports-6.0.0.jar!/:6.0.0]
        at net.sf.jasperreports.engine.fill.JRFiller.createBandReportFiller(JRFiller.java:187) ~[jasperreports-6.0.0.jar!/:6.0.0]
        at net.sf.jasperreports.engine.fill.JRFiller.createReportFiller(JRFiller.java:202) ~[jasperreports-6.0.0.jar!/:6.0.0]
        at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:110) ~[jasperreports-6.0.0.jar!/:6.0.0]
        at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:668) ~[jasperreports-6.0.0.jar!/:6.0.0]
        at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:984) ~[jasperreports-6.0.0.jar!/:6.0.0]
        at com.zjjk.professional.service.impl.OrderPrintServiceImpl.print(OrderPrintServiceImpl.java:111) ~[employee-management-1.0-SNAPSHOT.jar!/:1.0
-SNAPSHOT]
        at com.zjjk.professional.controller.OrderPrintController.printPDF(OrderPrintController.java:100) ~[employee-management-1.0-SNAPSHOT.jar!/:1.0-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_191]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_191]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_191]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_191]
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891) ~[spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) [spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) [spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) [spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) [spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]

之后再点击预览PDF按钮,报错如下

2019-08-01 09:22:08 [http-nio-10520-exec-5] ERROR cn.lancelot.framework.web.handler.ExceptionHandel 系统异常[1564622528684]
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class net.sf.jas
perreports.engine.util.JRStyledTextParser
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1006) [spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) [spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) [spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) [spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) [tomcat-embed-core-8.5.34.jar!/:8.5.34]
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) [spring-webmvc-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.34.jar!/:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.34.jar!/:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.34.jar!/:8.5.34]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.34.jar!/:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.34.jar!/:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.34.jar!/:8.5.34]
        at cn.lancelot.framework.web.filter.ChannelFilter.doFilter(ChannelFilter.java:35) [framework-common-2.0.6-RELEASE.jar!/:2.0.6-RELEASE]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.34.jar!/:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.34.jar!/:8.5.34]
        at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90) [spring-boot-actuator-2.0.6.RELEASE.jar!/:2.0.6
.RELEASE]

一开始同事让我解决的时候,只报第二个异常。当重新构建服务后,第一次出现的是Error initializing graphic environment.然后就一直报 java.lang.NoClassDefFoundError
经过一整天的,排查,和百度,最后在google的一个论坛上找到了正确的解决方法。

安装

RUN apk --update add fontconfig ttf-dejavu

后来又看了GIThup上的wiki问题,总结解决办法:
1.在构建docker镜像的试试,添加TTF。
2.使用新版的OPENJDK

具体思路

怀疑宿主机jdk环境和docker镜像jdk环境不一致;
https://github.com/docker-library/openjdk/issues/73
在上线网址观摩之后发现,相关的包可能会fontconfig在容器中调用。但是alpine镜像中的jdk环境好像没有这个fontconfig,因为JDK没有fontconfig不能加载出图片格式的文字。
解决方案

docker的alpine轻量级镜像包中没有fontconfig安装包,
只能在构建docker镜像的同时,将ttf-dejavu打包在镜像之中

在Dockerfile中添加以下内容
FROM fabric8/java-alpine-openjdk8-jdk

安装ttf-dejavu
RUN echo -e “https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main\n
https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/community” > /etc/apk/repositories

RUN apk --update add curl bash ttf-dejavu &&
rm -rf /var/cache/apk/*

构建新的镜像
在Dockerfile所在的目录输入命令:
docker build -t alpinejdk:3.2 .
再次启动

通过docker-compose编排工具启动
docker-compose.yaml
version: ‘3.1’
services:
mo-login:
image: alpinejdk:3.2
container_name: mo-login
privileged: true
restart: always
environment:
JAVA_OPTS: ‘-Xms256m -Xmx512m -Xss1024k’
network_mode: “host”
volumes:
- /opt/mo/mo-login/build/src:/opt/app
command: ["/bin/sh", “start.sh”]

启动并查看日志:docker-compose up -d ;docker-compose logs -f
正文到此结束
本文目录