我使用Jfreechart来创建图表。我正在创建chart的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"
感谢您的回复。
发布于 2012-07-06 10:07:48
您很可能希望在headless mode中调用您的Java程序
java -Djava.awt.headless=true -cp ... come.your.Class
或者,在运行时在任何图形代码之前(例如,在static {}
块中):
System.setProperty("java.awt.headless", "true");
https://stackoverflow.com/questions/11360088
复制