我在RedHat服务器上使用java.awt.HeadlessException
时收到Docx4jReplacedElementFactory
。有谁知道这个的解决方法? (除了运行Xvfb)
我已经深入了解堆栈跟踪,问题的根源是java.awt.GraphicsEnvironment.getDefaultScreenDevice()
,这会在无头环境中运行时抛出异常。 这是记录在案的:“throws:HeadlessException - 如果isHeadless()返回true”(请参阅对java.awt.HeadlessException thrown from HeadlessGraphicsEnvironment.getDefaultScreenDevice的评论)。
鉴于getDefaultScreenDevice
按指定工作,使用docx4j时是否有另一种方法可以避免去那里?
查看org.docx4j.org.xhtmlrenderer.util.ImageUtil.getGraphicsConfiguration
的来源,我们看到:
private static GraphicsConfiguration getGraphicsConfiguration() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
-> GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
return gc;
}
堆栈追踪:
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]