6

i running on Linux my junit test with openJDK 11 and got following error:

java.lang.ClassCastException: class sun.font.CompositeFont cannot be cast to class sun.font.PhysicalFont (sun.font.CompositeFont and sun.font.PhysicalFont are in module java.desktop of loader 'bootstrap')
    at java.desktop/sun.font.SunFontManager.getDefaultPhysicalFont(SunFontManager.java:1086)
    at java.desktop/sun.font.SunFontManager.initialiseDeferredFont(SunFontManager.java:965)
    at java.desktop/sun.font.SunFontManager.findOtherDeferredFont(SunFontManager.java:903)
    at java.desktop/sun.font.SunFontManager.findDeferredFont(SunFontManager.java:919)
    at java.desktop/sun.font.SunFontManager.findFont2D(SunFontManager.java:2120)
    at java.desktop/java.awt.Font.getFont2D(Font.java:506)
    at java.desktop/java.awt.Font.canDisplayUpTo(Font.java:2246)
    at java.desktop/java.awt.font.TextLayout.singleFont(TextLayout.java:469)
    at java.desktop/java.awt.font.TextLayout.<init>(TextLayout.java:530)
    at org.apache.poi.ss.util.SheetUtil.getDefaultCharWidth(SheetUtil.java:275)
    at org.apache.poi.xssf.streaming.AutoSizeColumnTracker.<init>(AutoSizeColumnTracker.java:117)
    at org.apache.poi.xssf.streaming.SXSSFSheet.<init>(SXSSFSheet.java:82)
    at org.apache.poi.xssf.streaming.SXSSFWorkbook.createAndRegisterSXSSFSheet(SXSSFWorkbook.java:658)
    at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:679)
    at com.textb2.ui.controller.ExcelCreator.newSheet(ExcelCreator.java:135)
    at com.textb2.ui.controller.ExcelCreator.buildExcel(ExcelCreator.java:182)
    at com.textb2.ui.controller.ExcelCreatorTest.testNoData(ExcelCreatorTest.java:91)
    at com.textb2.ui.controller.ExcelCreatorTest.testGermany(ExcelCreatorTest.java:53)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

Not sure but it must have something to do with fonts in /usr/share/fonts. At the moment i have no fonts there. I know that openJdk11 does not includes fonts anymore. But I'm not sure what to do.

Thanks ...

Just found a bug in an older version which is already fixed: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8144703

Simulant
  • 19,190
  • 8
  • 63
  • 98
Mathias Noack
  • 133
  • 1
  • 6
  • 2
    Add your code for the entire unit test in question. See: https://stackoverflow.com/help/mcve My guess is that you may have been using internal sun classes which are not guaranteed to exists between releases. This is also what happened with classloaders going from jdk8 to jdk11, the internal base classes and implementation classes changed and people casting to them got these type of failures. – AlexC Feb 06 '19 at 13:24

4 Answers4

4

It seems OpenJDK 11 does not include some font-related stuff any more and your operating system is currently not providing any by itself.

You can try to install some font-related packages in your operating system to see if that solves the problem.

See also the release notes at https://www.oracle.com/technetwork/java/javase/11-relnote-issues-5012449.html#JDK-8191522

centic
  • 15,565
  • 9
  • 68
  • 125
4

On Ubuntu 18.0.4 LTS, The following command fixed the issue

sudo apt-get install fontconfig

gavi
  • 2,104
  • 2
  • 16
  • 9
1

I think you must have some fonts installed, otherwise you would get a NullPointerException before getting to this point.

Does this simple program run okay,it would fail if no fonts installed ?

import java.awt.*;
public class Fonts
{
     public static void main(String[] args) throws Exception
     {
         Font defaultFont = Font.decode(null);
         System.out.println(defaultFont);
     }
}

I think the issue is more that your fonts are not configured correctly, its kind of clear that getDefaultPhysicalFont() expect to get a font of type PhysicalFont but is actually getting one of type CompositeFont, not clear why that is happening.

Try running

fc-cache -rv *pathoffonts*

e.g

fc-cache -rv /usr/share/fonts

that seemed worked for me when I add the same error also using Jakarta Poi, however Im not yet getting consistent results.

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
0

In our case we had a service running on K8s which would throw this error on the first request but would run fine for subsequent requests. Solved it by installing DejaVu in the base image of our Dockerfile, as some people recommended here.

Something like

FROM <my base image> AS EXPLODED
RUN apk add --update ttf-dejavu && rm -rf /var/cache/apk/*

Built the image, pushed it to Harbor and used the new image as a base image.

Bruno
  • 63
  • 7