Java in a slim or Alpine container fails the moment it needs a font
Your error
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11FontManager2 more lines it can showFix it fast
Most common cause, 1 of 2The runtime image has no FreeType or fontconfig libraries
Install the packages in the final stage, the one the container actually runs.
# Alpine base
RUN apk add --no-cache fontconfig ttf-dejavu
# Debian or Ubuntu base
RUN apt-get update \
&& apt-get install -y --no-install-recommends fontconfig \
&& rm -rf /var/lib/apt/lists/*Short answer
NoClassDefFoundError: Could not initialize class sun.awt.X11FontManager means Java's font code failed to load inside the container. A minimal image either lacks libfreetype and fontconfig, or has them but no font files. Install fontconfig and ttf-dejavu on Alpine, or fontconfig on Debian and Ubuntu, which pulls in a DejaVu font set. Official Temurin images already carry them.
Which cause do you have?
Look inside the image the container runs: first for the FreeType library, then for any font file. No library points to the first cause; a library with no fonts points to the second.
docker run --rm --entrypoint sh my-app -c "find / -name 'libfreetype.so*' 2>/dev/null | head -1"
docker run --rm --entrypoint sh my-app -c "find /usr/share/fonts -type f 2>/dev/null | head -3"Then find the line you see in this list and open its fix:
- 1The runtime image has no FreeType or fontconfig libraries
find returns no libfreetype.so.6, and the log shows the UnsatisfiedLinkError for libfreetype.so.6 or the X11FontManager error - 2fontconfig is present, but not a single font file is installed
RuntimeException: Fontconfig head is null; older JDK builds threw a NullPointerException from sun.awt.FontConfiguration for the same reason
What does the error look like?
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11FontManagerjava.lang.UnsatisfiedLinkError: libfontmanager.so: libfreetype.so.6: cannot open shared object file: No such file or directoryjava.lang.RuntimeException: Fontconfig head is null, check your fonts or fonts configuration
Cause 1 of 2
The runtime image has no FreeType or fontconfig libraries
You seefind returns no libfreetype.so.6, and the log shows the UnsatisfiedLinkError for libfreetype.so.6 or the X11FontManager error
Check
Could not initialize class sun.awt.X11FontManager inside a container usually means the base image never got the native font stack. The official Temurin Dockerfiles install fontconfig (plus ttf-dejavu on Alpine) and quote both errors in a comment as the reason. Images that skip that step hit them: a JDK copied into plain alpine or debian:slim, or a jlink runtime placed on a bare base.
grep -n -E '^FROM|COPY --from' DockerfileFix
Install the packages in the final stage, the one the container actually runs.
# Alpine base
RUN apk add --no-cache fontconfig ttf-dejavu
# Debian or Ubuntu base
RUN apt-get update \
&& apt-get install -y --no-install-recommends fontconfig \
&& rm -rf /var/lib/apt/lists/*Verify
The library now exists in the rebuilt image, and the PDF, chart or image call that crashed completes.
docker build -t my-app .
docker run --rm --entrypoint sh my-app -c "find / -name 'libfreetype.so*' 2>/dev/null | head -1"Confirmed by github.com
Cause 2 of 2
fontconfig is present, but not a single font file is installed
You seeRuntimeException: Fontconfig head is null; older JDK builds threw a NullPointerException from sun.awt.FontConfiguration for the same reason
Check
When the libraries load but no font exists, Java cannot build its font configuration. In 2023 OpenJDK replaced the bare NullPointerException from FontConfiguration.getVersion with the clearer Fontconfig head is null message, and the 17 and 21 update repositories carry the same text. Alpine is where this bites most: its fontconfig package depends on FreeType and expat, but on no font.
docker run --rm --entrypoint sh my-app -c "find /usr/share/fonts -type f 2>/dev/null | wc -l"Fix
Add a font package. On Debian and Ubuntu, fontconfig brings in fontconfig-config, which depends on fonts-dejavu-core or another font set, so name a font package there only when a later step deleted the fonts.
# Alpine
RUN apk add --no-cache ttf-dejavu
# Debian or Ubuntu
RUN apt-get update \
&& apt-get install -y --no-install-recommends fonts-dejavu-core \
&& rm -rf /var/lib/apt/lists/*Verify
Font files are found, and the code path that threw the exception renders text.
docker run --rm --entrypoint sh my-app -c "find /usr/share/fonts -name '*.ttf' | head -3"Confirmed by github.com
How do I stop it coming back?
- Build the runtime stage on an official eclipse-temurin JRE tag whenever the app draws text (PDFs, charts, thumbnails); those images ship the font packages already.
- Add a container smoke test that renders one line of text to a small PNG or PDF, so a slimmer base image cannot drop fonts unnoticed.
- Keep the font packages on the same RUN line as other runtime libraries, with a comment naming the feature that needs them, so a later clean-up does not remove them.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, Dispatch Pro.