NoClassDefFoundError: Could not initialize class sun.awt.X11FontManager
Asked Answered
R

4

29

when we migrated from Oracle JDK-8 to Open JDK-11, Apache POI excel generation has issues

tried already -Djava.awt.headless=true

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11FontManager
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1053)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:998)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:901)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)```
Raising answered 1/4, 2019 at 11:26 Comment(2)
javarevisited.blogspot.com/2011/06/…Brodie
we are using openJDK and cannot go back to oracle JDKRaising
F
63

If you are using the docker 'slim images' of openjdk, the image no longer includes the packages 'fontconfig', and 'libfreetype6'.

Check the difference in docker file of openjdk:11-slim and the docker file of openjdk:8-slim.

You could either pick another docker image (e.g. openjdk:11-windowsservercore), or install the packages yourself in your docker file:

RUN apt-get update && apt-get install -y fontconfig libfreetype6 && rm -rf /var/lib/apt/lists/*
Friction answered 8/11, 2019 at 11:19 Comment(6)
This should be the accepted answer. This worked perfectly when using the 'tomcat:9-jdk11-openjdk-slim' image.Equuleus
In docker file, updated "FROM library/openjdk:8-jdk-slim" to "FROM openjdk:8u232-jdk" fixed my problem. Thank you!Abc
What if I have alpine version? libfreetype6 is not present in alpine. I tried freetype instead but it doesn't work..Purge
@DenB For Alpine, I found no other way than installing libgcc (and I added the optional compatibility packages to atone for my sins) : apk add freetype-dev libgcc libc6-compat gcompatRomany
To minimize the Docker layer size, you may want to remove the apt-get update temporary files once you're done with the apt-get install step. You can do that by appending && rm -rf /var/lib/apt/lists/*. Also, it makes sense to join commands with && rather than ;, since you probably want to fail early if anything goes wrong with the update step.Haigh
Needed to add this to a project using openjdk:8-jre-slim, and the suggestion here worked. Note: if using a multi-stage docker build, be sure to add this at the correct stage.Preiser
R
3

The underlying issues is something else

I just created a simple class which does class loading of X11FontManager and when I ran it complained

unable to link "libfreetype6"

once I installed it using

apt-get install libfreetype6

started throwing could not link "libfontconfig1.so"

apt-get install libfontconfig1

that fixed the issue

Read this and understood few things https://medium.com/azulsystems/using-jlink-to-build-java-runtimes-for-non-modular-applications-9568c5e70ef4

Raising answered 3/4, 2019 at 11:33 Comment(0)
D
1

Problem occurs when you use openjdk docker image in alpine version for example :

adoptopenjdk/openjdk11:jre-11.0.11_9-alpine.

You need chenge image to full version: adoptopenjdk/openjdk11:jre-11.0.11_9

Distillation answered 10/12, 2021 at 14:29 Comment(0)
H
0
2021-06-23 03:44:37.950  INFO 1 --- [       Thread-7] ReportService   : Template path : /jasper/MgmtSummary.jrxml
Exception in thread "Thread-7" java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11FontManager
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398)

I was getting the above error when I was using docker image gcr.io/distroless/java-debian10:11

And this was fixed when I change the image to adoptopenjdk/openjdk11

Henhouse answered 4/7, 2021 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.