2

I am using:

Linux Debian 6, Java 1.6.29 and Tomcat 6

I've seen many posts about it explaining that java.awt requires X11 libraries..etc., but they are not solving the problem.

I set -Djava.awt.headless=true in my box and I worked around the first problem of the headless environment, now I am getting:

java.awt.HeadlessException
java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
java.awt.Window.(Window.java:432)
java.awt.Frame.(Frame.java:403)
javax.swing.JFrame.(JFrame.java:202)
org.jfree.ui.ApplicationFrame.(ApplicationFrame.java:65)
...

I know by doing this I just told java that there is no monitor (and in fact running a standalone java program to check if the environment is headless it does return true). So is it anything to do with the Linux environment like setting the DISPLAY env variable?

I would appreciate your help,

Thanks.

mzereba
  • 2,467
  • 5
  • 26
  • 40
  • 2
    Why do you need to use JFrame in Tomcat? – dash1e Apr 16 '12 at 08:11
  • yes exactly. If you notice the BarChartGenerator class at the beginning it extended the class ApplicationFrame. Now not anymore! that fixed the X11 dependency. – mzereba Apr 17 '12 at 10:47

5 Answers5

5

As you can read in http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/, many components are affected if a display device, keyboard or mouse is not supported. Methods related to Canvas, Panel, and Image components do not need to throw a HeadlessException because these components can be given empty peers and treated as lightweight components.

So the JFrame is the component throwing the HeadlessException.

If you want to generate charts with jfreechart in a headless environment, this link may help you: http://javaevangelist.blogspot.com.es/2010/11/creating-charts-on-headless-systems.html.

jalopaba
  • 8,039
  • 2
  • 44
  • 57
  • thank for ur reply, that's useful. is now not throwing the exception, but is creating a blank image (ill post some stuff when I can in 8 hrs). – mzereba Apr 16 '12 at 11:21
2
-Djava.awt.headless=false

add above it works a treat :)

Makky
  • 17,117
  • 17
  • 63
  • 86
1

You may need to install a VNC server (or something similar), as discussed in this forum thread.

Addendum: Instead of saving the chart image as a file, write to the server's output stream using one of the writeChartAsPNG() methods, as suggested here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

Take off any extends from ApplicationFrame will solve this problem. In most Tomcat deployment environments ,we usually has no X11 Windows package. we needn't a ApplicationFrame (JFram) which will use old AWT objects.

My example

BloodChart(String title)  extends ApplicationFrame 

changes to ->

BloodChart(String title)

It's work for me , good luck!

dimcookies
  • 1,930
  • 7
  • 31
  • 37
潘曉葦
  • 11
  • 1
0

Thanks for your reply.

Since you gave me that I've been trying to text it. Still doesn't work in the sense that: is now not throwing that exception anymore, but creating a blank image for some reason.

So I went from this:

public BarChartGenerator(org.qcri.power.ws.client.Server oServer, ServerStatistics oServerStat, List lVMs, String path) extends ApplicationFrame {

        System.setProperty("java.awt.headless", "true");
        boolean headless = GraphicsEnvironment.isHeadless();
        System.out.println("Headless: " + headless);
        Toolkit tk = Toolkit.getDefaultToolkit();
        tk.beep();

        // generate dataset
        final CategoryDataset dataset = createDataset(lVMs);
        setChart(createChart(oServer, oServerStat, lVMs, dataset));

        // create PNG of the chart...
        setFilename("chart_server"+oServer.getHost()+"_"+System.currentTimeMillis()+".gif");

        File fImageFile = new File(path+filename);
        try {
            ChartUtilities.saveChartAsPNG(fImageFile, chart, PowerInterface.CHART_WIDTH, PowerInterface.CHART_HEIGHT);
        } catch (Exception e) {
            e.printStackTrace();
        }
   }

to the following (getting rid of extends ApplicationFrame) and use the example you gave me:

public BarChartGenerator(org.qcri.power.ws.client.Server oServer, ServerStatistics oServerStat, List lVMs, String path) {

        System.setProperty("java.awt.headless", "true");
        boolean headless = GraphicsEnvironment.isHeadless();
        System.out.println("Headless: " + headless);
        Toolkit tk = Toolkit.getDefaultToolkit();
        tk.beep();

        // generate dataset
        final CategoryDataset dataset = createDataset(lVMs);
        setChart(createChart(oServer, oServerStat, lVMs, dataset));

        // create PNG of the chart...
        setFilename("chart_server"+oServer.getHost()+"_"+System.currentTimeMillis()+".gif");

        try {
            BufferedImage bufferedImage = getChart().createBufferedImage(PowerInterface.CHART_WIDTH, PowerInterface.CHART_HEIGHT);
            ImageIO.write(bufferedImage, "gif", new FileOutputStream(path+getFilename()));
        } catch (Exception e) {
            e.printStackTrace();
        }
   }

in the log it complains about ImageIO.write(bufferedImage, "gif", new FileOutputStream(path+getFilename())); giving this error (while the file is there!):

javax.imageio.IIOException: Can't create output stream!
        at javax.imageio.ImageIO.write(ImageIO.java:1560)
        at org.qcri.power.util.BarChartGenerator.<init>(BarChartGenerator.java:106)

Any clue?

mzereba
  • 2,467
  • 5
  • 26
  • 40