Search code examples
javajfreechart

Jfreechart without X11


I'm using Jfreechart for creating charts. I'm creating BufferedImage of chart and writing it in OutputStream.

It's working fine with display environment, but its Not working In headless environment.

Please help how do you make it work in such an environment.

Exception I got in log:

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)

And the part of catalina.sh I have edited:

#   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"

Thank you for your reply.


Solution

  • You most likely want to invoke your Java program in headless mode:

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

    Alternatively, at runtime before any graphics code (e.g. in a static {} block):

    System.setProperty("java.awt.headless", "true");