Pentaho/POI 抛出 InvocationTargetException

Pentaho/POI throws InvocationTargetException

我正在尝试将我的所有 java 应用程序升级到 openJDK11(来自 Oracle 8)。 我注意到使用 Pentaho 的报告服务器存在一个问题。

似乎 Excel autosize 功能需要在 openJDK11 中 libfontconfig.so,

但是我可以在 /usr/lib64/libfontconfig.so 找到模块。

有什么办法可以解决吗?

    2019/04/18 19:22:38 - GetReportJSon.0 - Finished reading query, closing connection.
    2019/04/18 19:22:38 - GetReportJSon.0 - Finished processing (I=1, O=0, R=0, W=2, U=0, E=0)
    java.lang.InternalError: java.lang.reflect.InvocationTargetException
                    at java.desktop/sun.font.FontManagerFactory.run(FontManagerFactory.java:86)
                    at java.base/java.security.AccessController.doPrivileged(Native Method)
                    at java.desktop/sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
                    at java.desktop/java.awt.Font.getFont2D(Font.java:497)
                    at java.desktop/java.awt.Font.canDisplayUpTo(Font.java:2246)
                    at java.desktop/java.awt.font.TextLayout.singleFont(TextLayout.java:469)
                    at java.desktop/java.awt.font.TextLayout.<init>(TextLayout.java:530)
                    at org.apache.poi.ss.util.SheetUtil.getDefaultCharWidth(SheetUtil.java:273)
                    at org.apache.poi.ss.util.SheetUtil.getColumnWidth(SheetUtil.java:248)
                    at org.apache.poi.ss.util.SheetUtil.getColumnWidth(SheetUtil.java:233)
                    at org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:551)
                    at org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:533)
                    at org.pentaho.di.trans.steps.excelwriter.ExcelWriterStep.closeOutputFile(ExcelWriterStep.java:244)
                    at org.pentaho.di.trans.steps.excelwriter.ExcelWriterStep.processRow(ExcelWriterStep.java:209)
                    at org.pentaho.di.trans.step.RunThread.run(RunThread.java:62)
                    at java.base/java.lang.Thread.run(Thread.java:834)
    Caused by: java.lang.reflect.InvocationTargetException
                    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
                    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
                    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
                    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
                    at java.desktop/sun.font.FontManagerFactory.run(FontManagerFactory.java:84)
                    ... 15 more
    Caused by: java.lang.NullPointerException
                    at java.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1262)
                    at java.desktop/sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:225)
                    at java.desktop/sun.awt.FontConfiguration.init(FontConfiguration.java:107)
                    at java.desktop/sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:719)
                    at java.desktop/sun.font.SunFontManager.run(SunFontManager.java:367)
                    at java.base/java.security.AccessController.doPrivileged(Native Method)
                    at java.desktop/sun.font.SunFontManager.<init>(SunFontManager.java:312)
                    at java.desktop/sun.awt.FcFontManager.<init>(FcFontManager.java:35)
                    at java.desktop/sun.awt.X11FontManager.<init>(X11FontManager.java:56)
                    ... 20 more
    2019/04/18 19:22:40 - WriteReportAuthor.0 - ERROR (version 7.0.0.1-7, build 1 from 2016-12-07 21.19.28 by buildguy) : Unexpected error
    2019/04/18 19:22:40 - WriteReportAuthor.0 - ERROR (version 7.0.0.1-7, build 1 from 2016-12-07 21.19.28 by buildguy) : java.lang.InternalError: java.lang.reflect.InvocationTargetException

更新:

在输入 '-Dsun.java2d.debugfonts=true' 之后,我得到了这个日志:

Looking for text fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.RedHat.7.2.properties
Looking for binary fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.RedHat.7.2.bfc
Looking for text fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.RedHat.7.properties
Looking for binary fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.RedHat.7.bfc
Looking for text fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.RedHat.properties
Looking for binary fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.RedHat.bfc
Looking for text fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.7.2.properties
Looking for binary fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.7.2.bfc
Looking for text fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.7.properties
Looking for binary fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.7.bfc
Looking for text fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.properties
Looking for binary fontconfig file : /opt/jdk11/jdk-11.0.1/lib/fontconfig.bfc
Did not find a fontconfig file.

通过做两件事解决(使用 docker 实现):

  1. 添加配置变量:

    这是让 JDK 知道 libfontconfig.so

  2. 的位置所必需的

ENV LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/lib/:/usr/lib64/"

  1. 将默认文件添加到 JDK/lib 文件夹:

    这是必需的,因为openjdk默认没有。 以下文件来自 Oracle JDK8

ADD ./src/main/resources/fontconfig.bfc /opt/jdk11/jdk-11.0.1/lib/fontconfig.bfc

ADD ./src/main/resources/fontconfig.properties.src /opt/jdk11/jdk-11.0.1/lib/fontconfig.properties.src

ADD ./src/main/resources/fonts /opt/jdk11/jdk-11.0.1/lib/fonts

更新:上述解决方案有效,但有时在加载 redhat linux 后,似乎无法找到字体并给出 "java.lang.ClassCastException: class sun.font.CompositeFont cannot be cast to class sun.font.PhysicalFont (sun.font.CompositeFont and sun.font.PhysicalFont are in module java.desktop of loader 'bootstrap')"。 10-30 分钟后,问题消失。

解决方案:

  1. 添加额外的 TTF 文件(对我来说是 calibri.ttf)并复制到 /usr/share/fonts/TTF

  2. 执行此命令:fc-cache -fv 这可确保字体配置立即加载字体。

ADD ./src/main/resources/fonts/*.ttf /usr/share/fonts/TTF/
RUN fc-cache -fv