Apache POI failing in createsheet due to missing fontconfig in Java 11 environments
Asked Answered
P

4

7

We are using Java 11 and compared to earlier versions of Java ,Java 11 have removed fonts folder from jre/lib. So our poi calls are failing at createsheet call,due to missing fontconfig. If we install fontconfig using yum ,it will work like charm.But management is not approving the same. Issue is not happening in Java8 as fonts are identified from jre/lib folder. Tried to put the fonts in current working directory ,but that also didnot help us. Can anyone let me know ,how poi picks up the fonts,from where and what I can do to overcome this issue.Also the list of fonts/files to get over the issue.

Regards, Rakhi

Plusch answered 6/8, 2021 at 1:59 Comment(5)
Poi relies on the Java runtime to handle fonts. This looks like a duplicate of issues like #43097282Placia
Can you add the full stacktrace of where it fails?Dhiman
in __gnu_cxx::__verbose_terminate_handler() () from /lib64/libstdc++.so.6 #10 0x00007fbd888b3a06 in ?? () from /lib64/libstdc++.so.6 #11 0x00007fbd888b3a33 in std::terminate() () from /lib64/libstdc++.so.6 #12 0x00007fbd888b3c53 in __cxa_throw () from /lib64/libstdc++.so.6 #13 0x00007fbd941de436 in PSJNI::JavaObject::CallVoidMethod(std::string const&, std::string const&, ...) const () from /opt/oracle/psft/pt/tools/bin/libpsjni.so #14 0x00007fbd93780b5e in CPSSpreadSheet::CreateSheet(wchar_t const*This didnot confirm issue is related to font,but worked with fontconfigPlusch
Have a look at wiki.archlinux.org/title/Java_Runtime_Environment_fonts (Font Selection and See Also sections)Placia
@Plusch did you find any fix?Bankhead
O
3

After diving a bit into the code in POI (version 5.2.3), I've found this in org.apache.poi.ss.util.SheetUtil:

/**
 * A system property which can be enabled to not fail when the
 * font-system is not available on the current machine
 */
private static final boolean ignoreMissingFontSystem =
        Boolean.parseBoolean(System.getProperty("org.apache.poi.ss.ignoreMissingFontSystem"));

Setting the system property org.apache.poi.ss.ignoreMissingFontSystem to true has resolved this issue for me:

// avoid exceptions on systems without MS Fonts
System.setProperty("org.apache.poi.ss.ignoreMissingFontSystem", "true")
Ostracism answered 24/11, 2023 at 13:59 Comment(0)
G
1

I finally resolved with 2 actions:

1. Setting system property:

  • org.apache.poi.ss.ignoreMissingFontSystem = true

2. Update to Apache POI 5.3.0

Problem already exists in Apache POI version 5.2.5 with Eclipse Temurin JDK 21

The code in SheetUtil has changed in version 5.3.0 and fixed the latest issue!


Maven:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.3.0</version>
</dependency>

Gradle:

implementation group: 'org.apache.poi', name: 'poi', version: '5.3.0'

Reference:

Glenn answered 9/8 at 21:15 Comment(5)
I have this issue also with apache poi 5.3.0 and JDK 21Retiform
@Retiform did you also set the system property org.apache.poi.ss.ignoreMissingFontSystem = true?Silly
no, I haven't tried that :)Retiform
You need do it too... After then I think finally the warning will gone ;)Silly
Tested with apache poi 5.3.0, eclipse temurin 21 and the property set, and it works.Compile
E
0

Unfortunately I faced today during tests execution on GitHub Actions runner: java.lang.RuntimeException: Fontconfig head is null, check your fonts or fonts configuration as well.

I use:

testImplementation "org.apache.poi:poi:5.3.0"
testImplementation "org.apache.poi:poi-ooxml:5.3.0"

Java21 Corretto Github runner is on Linux X64

Log:

SmthExcelConverterTest > should convert smth to excel FAILED
java.lang.RuntimeException: Fontconfig head is null, check your fonts or fonts configuration
at java.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1263)
at java.desktop/sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:221)
at java.desktop/sun.awt.FontConfiguration.init(FontConfiguration.java:105)
at java.desktop/sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:696)
at java.desktop/sun.font.SunFontManager$2.run(SunFontManager.java:352)
at java.desktop/sun.font.SunFontManager$2.run(SunFontManager.java:309)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:319)
at java.desktop/sun.font.SunFontManager.<init>(SunFontManager.java:309)
at java.desktop/sun.awt.FcFontManager.<init>(FcFontManager.java:35)
at java.desktop/sun.awt.X11FontManager.<init>(X11FontManager.java:55)
at java.desktop/sun.font.PlatformFontInfo.createFontManager(PlatformFontInfo.java:37)
at java.desktop/sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:51)
at java.desktop/java.awt.Font.getFont2D(Font.java:526)
at java.desktop/java.awt.Font.canDisplayUpTo(Font.java:2278)
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.getCellWidth(SheetUtil.java:262)
at org.apache.poi.ss.util.SheetUtil.getCellWidth(SheetUtil.java:223)
at org.apache.poi.ss.util.SheetUtil.getColumnWidthForRow(SheetUtil.java:399)
at org.apache.poi.ss.util.SheetUtil.getColumnWidth(SheetUtil.java:318)
at org.apache.poi.ss.util.SheetUtil.getColumnWidth(SheetUtil.java:295)
at org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:497)
at org.apache.poi.xssf.usermodel.XSSFSheet.autoSizeColumn(XSSFSheet.java:479)
at (some invocation of autoSizeColumn in my code)
Extragalactic answered 21/8 at 10:48 Comment(1)
Yes, exactly. Same issue. Also poi:5.3.0 and Java 21Retiform
R
0

This is not general answer to the question but could be helpful for somebody.
I had this issue when running apache poi from docker.
I had apache poi 5.3.0., JDK 21 and I tried azul/zulu-openjdk-alpine:21.0.1-jre or eclipse-temurin:21. What is neccessary to add to docker file is:

RUN apk add --no-cache fontconfig ttf-dejavu

Retiform answered 23/8 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.