java 无法在 JRE 8 中加载字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30626136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:24:18  来源:igfitidea点击:

Cannot load font in JRE 8

javafontsawtjava-8

提问by Khanetor

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.

我无法从 JRE 8 中的 S3 输入流加载字体。如果系统安装了 JRE 7、JDK 7 甚至 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(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.apply(App.scala:76)
        at scala.App$$anonfun$main.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.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.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(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.apply(App.scala:76)
        at scala.App$$anonfun$main.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.

因此,这不是 inputstream 的问题,而是 JRE 8 中字体本身的加载问题。

This seems like a bug in JRE 8 Font.

这似乎是 JRE 8 字体中的一个错误。

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).

我正在加载用于在图片中绘制字符串的字体。我的代码使用图像 java:7-jre (ok)、java:7-jdk (ok)、java:8-jre (fail)、java:8-jdk (ok) 在 Docker 容器中运行。

采纳答案by Khanetor

It turns out that this is a problem with the openjdk-8-jre-headlessinstallation. 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.

原来这是openjdk-8-jre-headless安装的问题。这是 Java 8 JRE 的 Docker 镜像中的安装。我只需安装openjdk-8-jre(没有无头),问题就消失了。

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

如果您查看错误日志,字体的加载需要 awt X11,这是headlessJRE 版本中缺少的。

回答by leoleozhu

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

我在使用 openjdk:8-jre-alpine 时遇到同样的错误。切换到 openjdk:8-jre 会有所帮助。

--- FROM openjdk:8-jre-alpine
+++ FROM openjdk:8-jre

回答by LazerBass

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-dejavupackage.

我们在使用 tomcat:8.0.38-jre8-alpine 时也遇到了这个错误。该图像缺少 fontconfig。您还可以安装ttf-dejavu软件包,而不是切换到不同的图像。

apk add --update ttf-dejavu

回答by Vertigo

On CentOS headless JRE is missing the fontconfigdependency:

在 CentOS 无头 JRE 上缺少fontconfig依赖项:

yum install fontconfig

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

另外一个可能需要至少安装一个字体(dejavuliberation,等)。

回答by Bhagwati Malav

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

对于 alpine 和 openjdk:使用RUN apk --update add fontconfig ttf-dejavu对我来说有效。

回答by Rax

For me this resolved issue:

对我来说,这个已解决的问题:

apt-get install -y libfontconfig1