Under a Unix operating system, like Linux, Solaris and others, The AWT toolkit relies on the X server
to run. For this reason, servlets using JetChart to generate charts do not run in a headless environment, like a dedicated server,
where there is not an X server installed.
This problem can be addressed using two different approaches. The first requires Xvfb, a pseudo X server that emulates
a display, providing the native graphic libraries the AWT toolkit relies on. Xvfb must be used with all JVMs prior to version 1.4.
The second approach is much simpler, but only can be used with JVM 1.4 and newer versions. Both options are detailed below.
The following lines show how to start Xvfb running as display 1. The screen size is set to 800x600 and depth to 8. The first
thing to be done is to create the DISPLAY environment variable.
# DISPLAY=localhost:1.0
# export DISPLAY
# Xvfb :1 -screen 0 800x600x8 &
Please refer to the Xvfb documentation for detailed instructions. There are versions of Xvfb available for different
Unix operating systems. Some Linux distributions usually install Xvfb as default.
Xvfb is available for download at http://www.x.org
Java version 1.4 comes with support for headless environment. The only configuration needed is to set the
system property java.awt.headless to true, in the init() method of a servlet, as follows:
System.setProperty("java.awt.headless","true");
Note: the ChartEncoder class, used to generate GIF,JPEG and PNG images, internally creates an
invisible Frame object. The constructor of heavyweight components implemented by JDK 1.4,
like the Frame class, throws a java.awt.HeadlessException. This exception is thrown when code
dependent on a valid display is called in an environment that does not support a display, and this is
the case of the ChartEncoder invisible Frame object.
Therefore, do not use the ChartEncoder class if you decide to generate encoded images setting the system
property java.awt.headless as described above. Instead, use the BufferedImage class in conjunction with
an explicit call to one of the image encoders available.