masato-san masato-san - 1 month ago 9
Java Question

jasper report - how to output value of Java variable?

I'm trying to test jasperreport and wrote Java code but it giving me error:

-------------------- PDF exception
net.sf.jasperreports.engine.JRException:
Error loading byte data : C:\Users\mt\Desktop\project20111116\test
\src3\report2_1336431810533_776931.class


What am I doing wrong?

Code:

public static void main(String[] args) {

try {

File jrxmlFile = new File("C:\\Users\\mt\\report2.jrxml");

if(jrxmlFile.exists()) {

//jrxml complie
JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlFile.getAbsolutePath());

List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();

for (int i = 0; i < 10; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("orderNumber", "hello");
map.put("anotherNumber", "world");
//map.put("canIUseArray", new String[]{"foo", "bar", "moo", "pee"});
list.add(map);
}


JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(list);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), dataSource);

JasperViewer.viewReport(jasperPrint, false);

}
}
catch(Exception e) {
System.out.println("-------------------- PDF exception ");
System.out.println(e);
}

System.out.println("DONE");

}//end main

Answer

It was due to jrxml not specifying language used.

In my case, I set it to "groovy" and the code worked fine.

(In jasperReport tag, added language="groovy" attribute)

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
name="report2" 
language="groovy"
pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="1.2100000000000006"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <style name="table">
Comments