SpringBoot Jar无法加载TrueType字体

时间:2019-07-11 07:07:51

标签: java spring-boot fonts

当我想使用字体时,发现了一个奇怪的行为,该字体包含在springboot胖罐中。在我的本地计算机上运行测试时,该计算机从/resources目录中加载了Font,它可以完美运行。但是,如果我使用maven构建应用程序并从终端运行它,那么我将收到:

java.io.IOException: Problem reading font data.
at java.awt.Font.createFont0(Font.java:1000)
at java.awt.Font.createFont(Font.java:877)

我试图找到一种解决方案,并执行了以下操作:

  • 使用不同的字体(有时会出现另一个有关表格或表损坏的错误)
  • 构建了各种版本的docker openjdk容器以测试行为
  • 构建一个ubuntu容器,安装openjdk
  • 使用了debian映像,安装了fc-cache并通过
    • /usr/share/fonts/truetype/europlate
    • fc-cache -f -v
  • 创建一个临时文件,只是为了确保该jar内没有访问问题。
  • 从我的终端运行了应用程序,加载字体也失败了
  • 使用字体可在编辑器上使用,甚至在应用程序从IDE运行时(无测试)

方法:

     public Font getFont() throws IOException, FontFormatException {
        File f = File.createTempFile("dang", "tmp");
        assert f != null;
        f.delete();
        ClassLoader classLoader = getClass().getClassLoader();
        Font font = Font.createFont(Font.TRUETYPE_FONT, classLoader.getSystemResourceAsStream("EuroPlate.ttf"));
        font.deriveFont(105f);
        System.out.println(font.getFontName());
        return font;
    }

编辑: IDE运行应用程序->有效

终端运行应用程序->失败

Stacktrace:

java.io.IOException: Problem reading font data.
    at java.desktop/java.awt.Font.createFont0(Font.java:1183)
    at java.desktop/java.awt.Font.createFont(Font.java:1052)
    at components.NumberPlateUtility.getFont(NumberPlateUtility.java:81)
    at components.NumberPlateUtility.completeImage(NumberPlateUtility.java:173)
    at main.NumberplateClientCommands.one(NumberplateClientCommands.java:63)
    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.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:223)
    at org.springframework.shell.Shell.evaluate(Shell.java:169)
    at org.springframework.shell.Shell.run(Shell.java:134)
    at org.springframework.shell.jline.InteractiveShellApplicationRunner.run(InteractiveShellApplicationRunner.java:84)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:783)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:773)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)
    at main.Main.main(Main.java:20)
    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.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

存储库:https://github.com/Semo/numberplate_generator.git

1 个答案:

答案 0 :(得分:1)

此代码将起作用:

public Font getFont() throws IOException, FontFormatException {
        File f = File.createTempFile("dang", "tmp");
        assert f != null;
        f.delete();
        ClassLoader classLoader = getClass().getClassLoader();
        Font font = Font.createFont(Font.TRUETYPE_FONT, classLoader.getResourceAsStream("EuroPlate.ttf"));
        font.deriveFont(105f);
        System.out.println(font.getFontName());
        return font;
    }

请注意classLoader.getResourceAsStream中的更改。选中此answer以获得更多说明。