Saturday, July 20, 2013

How to avoid "java.lang.NoClassDefFoundError: Could not initialize class" when using JFreeChart in a headless server

Recently I encountered following exception when using JFreeChart to generate a graphical chart in a product deployed in a weblogic server.

]] Root cause of ServletException.
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(
        at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(
        at sun.awt.X11.XToolkit.(
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(
        at java.awt.Toolkit$
        at Method)
        at java.awt.Toolkit.getDefaultToolkit(
        at sun.swing.SwingUtilities2$AATextInfo.getAATextInfo(
        at javax.swing.plaf.metal.MetalLookAndFeel.initComponentDefaults(
        at javax.swing.plaf.basic.BasicLookAndFeel.getDefaults(
        at javax.swing.plaf.metal.MetalLookAndFeel.getDefaults(
        at javax.swing.UIManager.setLookAndFeel(
        at javax.swing.UIManager.setLookAndFeel(
        at javax.swing.UIManager.initializeDefaultLAF(
        at javax.swing.UIManager.initialize(
        at javax.swing.UIManager.maybeInitialize(
        at javax.swing.UIManager.getDefaults(
        at javax.swing.UIManager.getColor(
        at org.jfree.chart.JFreeChart.(
        at org.jfree.chart.ChartFactory.createTimeSeriesChart(

During the issue investigation, I found out that the above mentioned Weblogic server is located inside a headless server. (A headless server is a server which does not have a monitor, a key board and a mouse. The control and access to a headless server is done using a network card (remote connection).) It turned out the reason for the above exception is that.

The problem happens because JFreeChart class has some static variables which are initialized using local graphic environment properties retrieved from javax.swing.UIManager.
javax.swing.UIManager in turn tries to get these default properties from local graphic environment.
But because there is no graphic environment in a headless server , initializing sun.awt.X11GraphicsEnvironment class (local graphic environment class) fails and then the loading of JFreeChart class also fails.

The solution for this problem is to add -Djava.awt.headless=true to the list of server start arguments of the managed server.  You can change it by,
  1. Login to the Weblogic admin console.
  2. Expand Environment and select Servers.
  3. In the Servers table, click the name of the managed server you want to configure.
  4. Select Configuration > Server Start.
If this is another server or some other java application, you can add this property to wherever place you can specify the JVM arguments for the start of that server or application.

Read more about Java headless mode from:


No comments: