Cannot load font in JRE 8
Asked Answered
C

9

24

I cannot load a font from an S3 Inputstream in JRE 8. I do not have issue if a system is installed with JRE 7, JDK 7, or even JDK 8.

val fontInputStream = s3Client.getObject(bucketName, objectKey).getObjectContent

val customFont = Font.createFont(Font.TRUETYPE_FONT, fontInputStream).deriveFont(Font.TRUETYPE_FONT, 20F)

The error that I got is

Exception in thread "main" java.io.IOException: Problem reading font data.
        at java.awt.Font.createFont0(Font.java:1000)
        at java.awt.Font.createFont(Font.java:877)
        at Main$.delayedEndpoint$Main$1(Main.scala:31)
        at Main$delayedInit$body.apply(Main.scala:11)
        at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
        at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
        at scala.App$$anonfun$main$1.apply(App.scala:76)
        at scala.App$$anonfun$main$1.apply(App.scala:76)
        at scala.collection.immutable.List.foreach(List.scala:381)
        at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
        at scala.App$class.main(App.scala:76)
        at Main$.main(Main.scala:11)
        at Main.main(Main.scala)

I tried to load the inputstream to a temp file, but it does not help. I also tried to load a font directly from a local file, but I got a different error with getting font metadata. Here is the error log.

Exception in thread "main" java.lang.NullPointerException
        at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
        at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
        at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
        at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:776)
        at sun.font.SunFontManager$2.run(SunFontManager.java:431)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.font.SunFontManager.<init>(SunFontManager.java:376)
        at sun.awt.X11FontManager.<init>(X11FontManager.java:57)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
        at java.lang.Class.newInstance(Class.java:442)
        at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
        at java.awt.Font.<init>(Font.java:614)
        at java.awt.Font.createFont(Font.java:1056)
        at Main$.delayedEndpoint$Main$1(Main.scala:32)
        at Main$delayedInit$body.apply(Main.scala:11)
        at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
        at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
        at scala.App$$anonfun$main$1.apply(App.scala:76)
        at scala.App$$anonfun$main$1.apply(App.scala:76)
        at scala.collection.immutable.List.foreach(List.scala:381)
        at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
        at scala.App$class.main(App.scala:76)
        at Main$.main(Main.scala:11)
        at Main.main(Main.scala)

So, this is not a problem with inputstream, but rather with the loading of the font itself in JRE 8.

This seems like a bug in JRE 8 Font.

I am loading a font to use to draw a string in a picture. My code is run in a Docker container using images java:7-jre (ok), java:7-jdk (ok), java:8-jre (fail), java:8-jdk (ok).

Chessa answered 3/6, 2015 at 16:50 Comment(2)
and what's the URL for the font? You may want to run it through TTX, and see if that throws any errors, as well as try loading it in Font Forge, which is even more finicky when it comes to telling you the font you're loading has bad values somewhere.Yenta
The openjdk blog explains this problem here: blog.adoptopenjdk.net/2021/01/…Babul
C
13

It turns out that this is a problem with the openjdk-8-jre-headless installation. This is the installation in the Docker image for java 8 JRE. I simply install openjdk-8-jre (without headless) and the problem goes away.

If you look at the error log, the loading of the font require awt X11, which is missing from headless version of JRE.

Chessa answered 4/6, 2015 at 17:19 Comment(1)
To further clarify the answer, the headless flavor of JRE does not support font loading because a headless environment is one without UI, thus no need for font. The non headless version, however, supports UI, thus supports font.Chessa
F
24

I get the same error with openjdk:8-jre-alpine. Switching to openjdk:8-jre helps.

--- FROM openjdk:8-jre-alpine
+++ FROM openjdk:8-jre
Fidler answered 20/11, 2017 at 6:22 Comment(1)
Switching from Alpine to jre solved this issue for me.Agentival
H
17

We also got that error when using tomcat:8.0.38-jre8-alpine. That image is missing the fontconfig. Instead of switching to a different image you could also install the ttf-dejavu package.

apk add --update ttf-dejavu
Hockenberry answered 11/6, 2018 at 16:23 Comment(0)
C
13

It turns out that this is a problem with the openjdk-8-jre-headless installation. This is the installation in the Docker image for java 8 JRE. I simply install openjdk-8-jre (without headless) and the problem goes away.

If you look at the error log, the loading of the font require awt X11, which is missing from headless version of JRE.

Chessa answered 4/6, 2015 at 17:19 Comment(1)
To further clarify the answer, the headless flavor of JRE does not support font loading because a headless environment is one without UI, thus no need for font. The non headless version, however, supports UI, thus supports font.Chessa
A
13

For alpine and openjdk : Use RUN apk --update add fontconfig ttf-dejavu Worked for me.

Andrey answered 19/6, 2019 at 9:39 Comment(0)
G
11

On CentOS headless JRE is missing the fontconfig dependency:

yum install fontconfig

Also one might need to install at least one font (dejavu, liberation, etc).

Glennisglennon answered 20/9, 2016 at 15:6 Comment(0)
B
6

For me this resolved issue:

apt-get install -y libfontconfig1
Brookner answered 28/7, 2016 at 15:33 Comment(1)
For Docker add these line to remove this issue : RUN apt-get update && \ apt-get install -y libfontconfig1Blowbyblow
K
1

I spent days suffering with it until I saw this github thread and made the night happy. it's practically calling GraphicsEnvironment again

link here

Kennie answered 19/8, 2020 at 2:25 Comment(0)
W
0

Fonts are no longer part of JDK 11, fonts installed on host should be used now. so, you need to install and keep fontconfig in classpath.

More detail: https://mcmap.net/q/581668/-java-11-does-not-have-fonts

Waltner answered 9/10, 2023 at 13:7 Comment(0)
U
0

We faced this problem when we moved from oracle jdk8 to openjdk 1.8.0_392 on centOS 6 We had to install :

sudo yum -y install fontconfig freetype dejavu-sans-fonts

Then you can check your fonts using :

fc-list
Ulema answered 29/1 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.