jfreechart and -Djava.awt.headless=true ?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
pcouas
Posts: 9
Joined: Wed Feb 14, 2007 7:10 pm

jfreechart and -Djava.awt.headless=true ?

Post by pcouas » Thu Feb 22, 2007 5:21 pm

Hi

Could i running jfreechart with tomcat usign JDK1.4.2 in mode -Djava.awt.headless=true
Regards
Philippe

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: jfreechart and -Djava.awt.headless=true ?

Post by david.gilbert » Thu Feb 22, 2007 5:22 pm

pcouas wrote:Could i running jfreechart with tomcat usign JDK1.4.2 in mode -Djava.awt.headless=true
Did you try? It should work fine.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

pcouas
Posts: 9
Joined: Wed Feb 14, 2007 7:10 pm

Post by pcouas » Thu Feb 22, 2007 5:43 pm

I have tried and i have following error message

java.awt.HeadlessException at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:149)

My sample JSP Page
<%@page contentType="text/html; charset=Big5"

import="org.jfree.chart.ChartFactory"
import="org.jfree.chart.ChartFrame"
import="org.jfree.chart.JFreeChart"
import="org.jfree.chart.plot.*"
import="org.jfree.data.general.DefaultPieDataset"

%>

<%
// create a dataset...
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Category 1", 43.2);
dataset.setValue("Category 2", 27.9);
dataset.setValue("Category 3", 79.5);

// create a chart...
JFreeChart chart = ChartFactory.createPieChart(
"Sample Pie Chart",
dataset,
true, // legend?
true, // tooltips?
false // URLs?
);

// create and display a frame...
ChartFrame frame = new ChartFrame("First", chart);
frame.pack();
frame.setVisible(true);

%>

pcouas
Posts: 9
Joined: Wed Feb 14, 2007 7:10 pm

Post by pcouas » Thu Feb 22, 2007 5:45 pm

complete error message is
java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:149)
at java.awt.Window.<init>(Window.java:300)
at java.awt.Frame.<init>(Frame.java:427)
at javax.swing.JFrame.<init>(JFrame.java:219)
at org.jfree.chart.ChartFrame.<init>(ChartFrame.java:78)
at org.jfree.chart.ChartFrame.<init>(ChartFrame.java:66)
at org.apache.jsp.jsp02_jsp._jspService(jsp02_jsp.java:64)

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Thu Feb 22, 2007 5:50 pm

ChartFrame is a subclass of JFrame - you don't want to be creating instances of Swing components in your server-side application.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Taqua
JFreeReport Project Leader
Posts: 698
Joined: Fri Mar 14, 2003 3:34 pm
Contact:

Post by Taqua » Thu Feb 22, 2007 5:53 pm

Creating a frame on a server? Are you sure that you are currently writing a Web-Application?

Or is this some sort of remote control for the Web-Server admin - the users somewhere in the world open up new Frames on the server and the admin has to close them as fast as possible :)

Seriously: Web-Applications cannot have a Swing-GUI. You have to create HTML (through JSP pages) and images (by encoding and streaming the chart) on the server and then you have to rely on the browser to display everything correctly. If that's new to you or you haven't read the underlying standards yet, then this is a good time to do this. Grab a book on Web-Development and learn the basics, it will save you a lot of trouble later. (Web-Development has driven many upright programmers into insanity - dont take this task too lightly).

You certainly want to stream the image data to the client so that the client's browser can display the image. 'ChartUtilities.writeChartAsPNG' may serve as a starting point here.

Have fun,
said Thomas

Locked