2

我正在使用 Jfreechart 创建图表。我正在创建BufferedImage图表并将其写入OutputStream.

它在显示环境中工作正常,但在headless环境中不工作。

请帮助您如何使其在这样的环境中工作。

我进入日志的异常:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/WebAdmin] threw exception [Handler processing failed; nested exception is java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.] with root cause
java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
    at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
    at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65)
    at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:110)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:74)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:102)
    at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:81)
    at sun.swing.SwingUtilities2.isLocalDisplay(SwingUtilities2.java:1457)
    at javax.swing.plaf.metal.MetalLookAndFeel.initComponentDefaults(MetalLookAndFeel.java:1556)
    at javax.swing.plaf.basic.BasicLookAndFeel.getDefaults(BasicLookAndFeel.java:148)
    at javax.swing.plaf.metal.MetalLookAndFeel.getDefaults(MetalLookAndFeel.java:1592)
    at javax.swing.UIManager.setLookAndFeel(UIManager.java:536)
    at javax.swing.UIManager.setLookAndFeel(UIManager.java:576)
    at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1345)
    at javax.swing.UIManager.initialize(UIManager.java:1455)
    at javax.swing.UIManager.maybeInitialize(UIManager.java:1422)
    at javax.swing.UIManager.getDefaults(UIManager.java:656)
    at javax.swing.UIManager.getColor(UIManager.java:698)
    at org.jfree.chart.JFreeChart.<clinit>(JFreeChart.java:263)
    at org.jfree.chart.ChartFactory.createBarChart(ChartFactory.java:893)

我编辑的 catalina.sh 部分:

#   JSSE_HOME       (Optional) May point at your Java Secure Sockets Extension
#                   (JSSE) installation, whose JAR files will be added to the
#                   system class path used to start Tomcat.
#
#   CATALINA_PID    (Optional) Path of the file which should contains the pid
#                   of catalina startup java process, when start (fork) is used
#
# $Id: catalina.sh 609438 2008-01-06 22:14:28Z markt $
# -----------------------------------------------------------------------------

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m 
-Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m 
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"

感谢你的回复。

4

1 回答 1

4

您很可能希望以无头模式调用您的 Java 程序:

java -Djava.awt.headless=true -cp ... come.your.Class

或者,在任何图形代码之前的运行时(例如在一个static {}块中):

System.setProperty("java.awt.headless", "true");
于 2012-07-06T10:07:48.390 回答