3

我正在尝试测试 jasperreport 并编写 Java 代码,但它给了我错误:

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

我究竟做错了什么?

代码:

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
4

1 回答 1

2

这是由于 jrxml 没有指定使用的语言。

就我而言,我将其设置为“groovy”并且代码运行良好。

(在 jasperReport 标签中,添加了language="groovy"属性)

<?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">
于 2012-05-07T23:32:59.657 回答