|
use ChartDirector with Groovy |
Posted by Fulvio Biondi on Jun-23-2011 17:35 |
|
Dear all, I'd like to use ChartDirector in groovy.
The scenario is as follow :
a java application uses the standard scripting mechanism to invoke
a groovy script which should generate an image on disk.
I've started from one of the available example provided, and using
standard java it works just fine.
When I try to 'port' the same code in groovy I get the following error :
Exception in thread "main" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/jsp/PageContext
My test application is standalone. Could you help me to understand where I'm wrong ?
This is the groovy script segment (perhaps the error is here)
// The data for the line chart
data = [400,600,300,100,900]
// The labels for the line chart
labels = ["2008", "2009", "2010", "2011"]
// Create a XYChart object of size 300 x 280 pixels
def c = new XYChart(300, 280);
// Set the plotarea at (45, 30) and of size 200 x 200 pixels
c.setPlotArea(35, 45, 200, 200);
// Add a title to the chart using 12 pts Arial Bold Italic font
c.addTitle("This is my groovy test", "Arial Bold Italic", 10);
// Add a blue (0x6666ff) 3D line chart layer using the give data
c.addLineLayer(data, 0x6666ff).set3D();
// Set the labels on the x axis.
c.xAxis().setLabels(labels);
// Display 1 out of 3 labels on the x-axis.
c.xAxis().setLabelStep(3);
flag = c.makeChart("/home/fbi/chart.svg");
Thank you very much for any hint.
Regards
Fulvio |
Re: use ChartDirector with Groovy |
Posted by Peter Kwan on Jun-24-2011 03:43 |
|
Hi Fulvio,
I have never used Groovy before, but I will try to guess what is the cause.
The javax/servlet/jsp/PageContext is a class in J2EE (Java Enterprise Edition). It is typically used in JSP web applications. The error means that your copy of the J2EE is incomplete. However, in theory, javax/servlet/jsp/PageContext should not be needed in your application at all, so whether the J2EE exists or not should not matter.
ChartDirector is designed for both standalone and web applications. So ChartDirector contains code to use J2EE as well as J2SE (Java Standard Edition). According to Java standard, if there is J2EE code, but the code is not actually executed, then there is no need to have J2EE in the system.
For your case, it is not a web application, so J2EE is not needed. As according to your information, the code does run normally in standard Java.
However, I have seen an alternative Java implementation gcj (which compiles Java byte code to native machine code) that insists on finding all methods referenced in all JAR files, even if they are not used. This is in violation of the Java standard. The result is that for gcj to work with ChartDirector, the developer must hava a valid J2EE.
I am guessing something similar is happening in Groovy, so it insists on requiring a valid J2EE. Normally, if there is no J2EE, there will be a "Class Not Found" error. However, in your case, the error is "java.lang.ClassFormatError", which means the J2EE implementation is incomplete.
If you download the J2EE implementation from SUN (or other Java VM brands), the J2EE should be complete. However, from Internet search, some development tools (such as "Maven") intentionally include an imcomplete J2EE. The incomplete J2EE is like a "header file" in C, which contains the method signature, but not the actual code. This allows the Java code to compile, but not to run. This can cause the java.lang.ClassFormatError in your case.
If my guess is correct, to solve the problem, you may try one of the following methods:
(a) Use a tool that complies to the Java standard (which is not to load the J2EE if it is not actually used at runtime). If Groovy cannot do this, you may need to use an alternative development tool, such as standard Java.
(b) Use a valid J2EE implementation to satisfy Groovy. You may download a free J2EE from SUN (or IBM or other brands of Java). Please make sure in your Java class path, you are referencing the complete J2EE, and not the incomplete J2EE.
Hope this can help.
Regards
Peter Kwan |
|