2

I am trying to migrate a flex application using the jasper reports library over to the java 8 runtime and am running into an error trying to load fonts to embed in a PDF report. I think it is related to a headless openjdk but I can't tell since it is on Google App Engine.

net.sf.jasperreports.engine.JRRuntimeException: Error initializing graphic environment. (Logger.java:137)
    at net.sf.jasperreports.engine.util.JRGraphEnvInitializer.initializeGraphEnv(JRGraphEnvInitializer.java:63)
    at net.sf.jasperreports.engine.fill.BaseReportFiller.<init>(BaseReportFiller.java:124)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:268)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:69)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:57)
    at net.sf.jasperreports.engine.fill.JRFiller.createBandReportFiller(JRFiller.java:219)
    at net.sf.jasperreports.engine.fill.JRFiller.createReportFiller(JRFiller.java:234)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:103)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:456)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:863)

...snip
Caused by: java.lang.NullPointerException
    at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
    at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
    at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
    at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774)
    at sun.font.SunFontManager$2.run(SunFontManager.java:431)

Possibly related Cannot load font in JRE 8

Has anyone experienced this and have a workaround? I am currently using flex, but would like to move to standard if possible.

EDIT: This was the fix that made it work

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <!-- These will be managed using maven filters for local execution and proper local datastore namespacing -->
  <application>@appId@</application>
  <version>@appVersion@</version>
  <service>report</service>
  <runtime>java8</runtime>
  <system-properties>
    <property name="sun.awt.fontconfig" value="/base/jre8/lib/fontconfig.Prodimage.properties"/>
  </system-properties>
</appengine-web-app>

Thanks @dsesto!

Josh J
  • 6,813
  • 3
  • 25
  • 47
  • As per [the documentation](https://cloud.google.com/appengine/docs/standard/java/images/), Java 8 on App Engine does have support for AWT and Java2D, so there should be no compatibility issues. Just for clarification, is your application working properly in a different environment? Can you share the configuration of your App Engine 8 application and the piece of code that triggers the error you shared? – dsesto Jan 19 '18 at 12:16
  • 1
    Plus [this other SO post](https://stackoverflow.com/questions/45100138/how-to-configure-google-appengine-to-work-with-vector-graphic) looks exactly like your error, so you can have a look at the suggested answer. – dsesto Jan 19 '18 at 12:19
  • @dsesto it works correctly in the flex runtime, and formerly on managed VMs. I assumed it had something to do with fonts as I can't recreate it in the local devappserver and a debugger is nearly impossible once deployed. Let me check if that other solution fixes it and get back to you. – Josh J Jan 19 '18 at 14:53
  • So which is the environment in which you got it working? App Engine Flexible? Which runtime environment did you use for that? Or did I missunderstood and your error happens in Standard? – dsesto Jan 19 '18 at 15:03
  • The error happens in java 8 standard, it works correctly with `flex` @dsesto – Josh J Jan 19 '18 at 15:26
  • @dsesto you were right. Setting the `sun.awt.fontconfig` property fixed it for me. It would have been nice if the stack trace / error message brought up that [SO post](https://stackoverflow.com/a/47107245/1182891) you mentioned. Thank you! If you want some points, I'll accept an answer if you write one up. – Josh J Jan 19 '18 at 16:28
  • 1
    I will add an answer for future people who open this question and go directly to the "Answers" section :) – dsesto Jan 19 '18 at 16:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163505/discussion-between-josh-j-and-dsesto). – Josh J Jan 19 '18 at 16:57

2 Answers2

2

This other Stack Overflow post has the exact same error as the one you share, so you can try with the solution provided there.

It will require you to add the sun.awt.fontconfig system property.

OP updated his question with the edit that made it work following the suggestion in that other SO post, so you should check the question edit for the answer.

dsesto
  • 7,864
  • 2
  • 33
  • 50
  • I opened a feature request https://issuetracker.google.com/issues/72216727 to see if there is a platform wide solution that can be made. – Josh J Jan 19 '18 at 17:07
2

The XML property didn't work for me. Here is what did (Kotlin code):

    fun onStart() {
        val libDir = File(System.getProperty("java.home"), "lib")
        val fileList = libDir.list()?.toList() ?: listOf()
        val fontString = fileList.firstOrNull { it.contains("fontconfig") }
        if(fontString != null) {
            ErrorLog.logError("Found fontconfig file $fontString")
            val fontFile = File(libDir, fontString)
            System.setProperty("sun.awt.fontconfig", fontFile.path)
        }
    }
Clyde
  • 7,389
  • 5
  • 31
  • 57