1

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]
Community
  • 1
  • 1
David Carboni
  • 1,556
  • 23
  • 24

1 Answers1

1

You could apply this recent upstream patch

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • Perfect. I was able to override that one class with a version that included the patch and it's now working. Based on the line `if (GraphicsEnvironment.isHeadless())` it was looking good from the start. Many thanks. – David Carboni Jul 25 '12 at 10:30
  • I'm slightly confused by the package structure in this solution. I think it needs to be modified to `org.docx4j.org.xhtmlrenderer.util` as this is the version of the class used – Edd Aug 03 '12 at 16:42
  • The fix is present in http://www.docx4java.org/docx4j/xhtmlrenderer-nightly-20130903.jar – JasonPlutext Sep 03 '13 at 01:52
  • Hi Jason I cant find that copy of the xhtmlrenderer jar. Is it stored anywhere else? – snowstreams Nov 12 '13 at 11:24