Jump to content
Changes to the Jaspersoft community edition download ×

Programmatically placing an Image in a band

Recommended Posts

By: Andrew Matthews - namxat

Programmatically placing an Image in a band

2003-06-05 12:46

Hello. I need to programmatically create a JasperDesign object ( as in a dori.jasper.engine.JasperDesign object, NOT an XML flat file ), embed a java.awt.Image in the detail ( via the setDetail method of JasperDesign), and then compile, fill, and print the report.


Here's what I have and know how to do :


-- I know how to obtain the java.awt.Image.


-- I know how to programmatically create the JasperDesign object.


-- I know how to compile, fill, and print the JasperDesign object.


What I need to know is this: how do I associate my image with the detail band? Consider the following code:


public void makeReport ( java.awt.Image image ) {

JasperDesign jd = new JasperDesign();

JRDesignBand detailBand = new JRDesignBand();

JRDesignImage designImage =

new JRDesignImage();



* Mystery code here: I can only assumethat this

* where I associate my java.awt.Image with

* my JRDesignImage object. The API documentation

* is sparse, as is the PDF documentation.









WHAT I WANT TO DO: I do NOT want to resort to using a servlet to do this; I am looking for the minimal amount of code to associate a java.awt.Image object with a JRDesignImage object.





By: Andrew Matthews - namxat

Now I'm in NoClassDefFoundError hell...

2003-06-09 13:55

Hello. I figured out ( almost ) how to do what I want. Consider the following code:


JRDesignExpression expression = new JRDesignExpression();


Image chartImg = (Image)chart.createBufferedImage( width, height );

int imgID = ImageRegistry.add( chartImg );

expression.setText( "com.myPackage.ImageRegistry.get(" +

imgID + ", true)" );

JRDesignBand detail = new JRDesignBand();

detail.setHeight( height );

image.setExpression( expression );

detail.addElement( image );

jd.setDetail( detail) ;


Now, to clarify a couple of things: chart is the JFreeChart, obviously. ImageRegistry is a static class that allows me to register and then retrieve images. I compile this, and it compiles fine. When I run it, however, I get the following output:



* WARNING: Unable to compile with "sun.tools.javac.Main" compiler. *

* Cause : java.lang.ClassNotFoundException : sun.tools.javac.Main *

* Compiling with command line "javac" compiler. *


java.lang.NoClassDefFoundError: com/myPackage/ImageRegistry



Now here's the deal: I am used to seeing the warning message, although it is somewhat disconcerting. the NoClassDefFoundError, however, I don't see how I am generating. I have checked the classpath serveral times. I have set the "java.ext.dirs" system property, as well. I even went so far as adding the following code:


String classpath = System.getProperty( "java.class.path" );

String pathSep = System.getProperty("path.separator");

String fileSep = System.getProperty("file.separator");

classpath += pathSep + System.getProperty("java.library.path");

classpath += " -Djava.ext.dirs=" + System.getProperty("java.ext.dirs");

classpath += pathSep +

System.getProperty("java.home") + fileSep + "lib";

System.setProperty( "java.class.path", classpath );


Still, the the warning and the NoClassDefFoundError kept rearing their ugly heads.


Now, I should probably say that I try to avoid LIKE THE PLAGUE hard-coding references to .jar archives. Also, given the large number of .jar archives our company uses, I also try to avoid LIKE THE PLAGUE having to explicitly add every single .jar archive to the classpath. Instead, we store all our .jar archives in

one directory, and then simply add the "-Djava.ext.dirs" directive as one of our java flags.


What do I need to do to get rid of these warnings and errors?





By: Andrew Matthews - namxat

This is on a Windows 2000 PC, by the way.

2003-06-09 14:01

Just thought I should mention that little fact. I forgot to in my post.





By: Emil Einarsson - eeinarsson

RE: Programmatically placing an Image in a band

2003-06-11 01:51

You might need to set the classpath in the system property "jasper.reports.compile.class.path" in order to get this to work, as the compiler spawns a new javac process, which does not register the programmatic changes to the class path.


Good luck, regards

Emil Einarsson





By: Andrew Matthews - namxat

Still not working...

2003-06-11 04:44

Thanks for taking a crack at it, Emil, but it's still not working. I added the following two lines of code:


String classpath = System.getProperty("java.class.path");

System.setProperty("jasper.reports.compile.class.path", classpath);


I still get the same NoClassDefFoundError: com/mypackage/ImageRegistry


I copied my tools.jar archive into the same directory as the JasperReports jar, and got rid of the warning, though.


I do not appear to be having any problem actually compiling the report; it's filling it that is giving me grief. Here is a ( partial ) stack trace:


java.lang.NoClassDefFoundError: com/mypackage/ImageRegistry

at Chart.evaluate(Chart.java:106)

at dori.jasper.engine.fill.JRCalculator.evaluate(JRCalculator.java:678)


Incidentally: I do not think it matters, but I should probably mention that my com.mypackage.ImageRegistry object is not a scriptlet; it is a singleton with a collection of public methods.


Does anybody have any ideas on what's causing the fill process to throw a NoClassDefFoundError, or how to correct it?






By: Emil Einarsson - eeinarsson

RE: Still not working...

2003-06-11 06:32

I have managed to recreate your problem exactly, and I am as puzzled as you are.


I think (pursuing this at the moment) that the issue is related to how the resulting report class file is loaded by the classloader.


I'm working with jasperreports as part of a large Eclipse plugin project, so class loading issues arise from time to time.





By: Emil Einarsson - eeinarsson

RE: Programmatically placing an Image in a band

2003-06-11 07:10

Ok. This issue is the same as the one discussed in the thread "Suggestion: a solution to JRClassLoader issue".


See that thread for more information!









By: Teodor Danciu - teodord

RE: Programmatically placing an Image in a band

2003-07-07 12:35




This has been solved in the new 0.5.0 version.


Thank you,


Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...