Frage

I'm getting a java.awt.HeadlessException when using a Docx4jReplacedElementFactory on a RedHat server. Does anyone know of a workaround for this? (other than running Xvfb)

I've drilled in to the stack trace and the source of the problem is java.awt.GraphicsEnvironment.getDefaultScreenDevice(), which throws the exception when running in a headless environment. This is as documented: "throws: HeadlessException - if isHeadless() returns true" (see comment on java.awt.HeadlessException thrown from HeadlessGraphicsEnvironment.getDefaultScreenDevice).

Given getDefaultScreenDevice is working as specified, is there another approach when using docx4j that avoids going there?

Looking at the source of org.docx4j.org.xhtmlrenderer.util.ImageUtil.getGraphicsConfiguration, we see:

private static GraphicsConfiguration getGraphicsConfiguration() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  -> GraphicsDevice gs = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gs.getDefaultConfiguration();
    return gc;
}

Stack trace:

Cause details:
java.awt.HeadlessException
    at sun.java2d.HeadlessGraphicsEnvironment.getDefaultScreenDevice(HeadlessGraphicsEnvironment.java:82)
    at org.docx4j.org.xhtmlrenderer.util.ImageUtil.getGraphicsConfiguration(ImageUtil.java:119)
    at org.docx4j.org.xhtmlrenderer.util.ImageUtil.makeCompatible(ImageUtil.java:70)
    at org.docx4j.org.xhtmlrenderer.swing.AWTFSImage.createImage(AWTFSImage.java:38)
    at org.docx4j.org.xhtmlrenderer.swing.NaiveUserAgent.createImageResource(NaiveUserAgent.java:197)
    at org.docx4j.org.xhtmlrenderer.swing.NaiveUserAgent.getImageResource(NaiveUserAgent.java:167)
    at org.docx4j.org.xhtmlrenderer.docx.Docx4jReplacedElementFactory.createReplacedElement(Docx4jReplacedElementFactory.java:57)
[snip]
War es hilfreich?

Lösung

You could apply this recent upstream patch

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top