javadockerjvmheadlessalpine-linux

no fontmanager in java.library.path


The following code works just fine on my desktop:

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setFont(new Font("SansSerif", Font.BOLD, 18));

        Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics.drawString(s, 5, 20);

However, when I run this code on my server (openjdk running on linux alpine within a Docker container), it fails with the following error:

java.lang.UnsatisfiedLinkError: no fontmanager in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at sun.font.FontManagerNativeLibrary$1.run(FontManagerNativeLibrary.java:61)
at java.security.AccessController.doPrivileged(Native Method)
at sun.font.FontManagerNativeLibrary.<clinit>(FontManagerNativeLibrary.java:32)
at sun.font.SunFontManager$1.run(SunFontManager.java:339)
at java.security.AccessController.doPrivileged(Native Method)
at sun.font.SunFontManager.<clinit>(SunFontManager.java:335)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:82)
at java.security.AccessController.doPrivileged(Native Method)
at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
at java.awt.Font.getFont2D(Font.java:491)
at java.awt.Font.access$000(Font.java:224)
at java.awt.Font$FontAccessImpl.getFont2D(Font.java:228)
at sun.font.FontUtilities.getFont2D(FontUtilities.java:180)
at sun.java2d.SunGraphics2D.checkFontInfo(SunGraphics2D.java:669)
at sun.java2d.SunGraphics2D.getFontInfo(SunGraphics2D.java:830)
at sun.java2d.pipe.GlyphListPipe.drawString(GlyphListPipe.java:50)
at sun.java2d.pipe.ValidatePipe.drawString(ValidatePipe.java:165)
at sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2928)

java -version (on my server) gives:

openjdk version "1.8.0_77-internal"
OpenJDK Runtime Environment (build 1.8.0_77-internal-alpine-r0-b03)
OpenJDK 64-Bit Server VM (build 25.77-b03, mixed mode)

I searched for awt libs and they could be found here:

/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/libawt_headless.so
/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/libawt.so

Solution

  • I ran into this exact same problem running Alpine Linux 3.4. I solved the problem by following the directions in this github issue:

    1. Install the JRE with GUI support, i.e. openjdk8-jre
    2. Install the fontconfig and ttf-dejavu packages

    The RUN line in my Dockerfile looks like:

    RUN apk add openjdk8-jre fontconfig ttf-dejavu
    

    After this, the JDK was able to load the default fonts automatically, and the exception went away.