问题
I want to include the .jrxml file in my NetBeans swing project. I use NetBeans 7.0.1. I created a package inside source package called "rep" and created a simple .jrxml file called "rp.jrxml". I have installed the iReport plugin in NetBeans. When I set a outer .jrxml file, it is showed ("D:/MyReports/firstreport.jrxml") but when I set the NetBeans package path, it was not shown. Here is my code.
try {
String reportSource="/rep/rp.jrxml"; //and also "rep/rp.jrxml" is used.no result.
Map<String, Object> params = new HashMap<String, Object>();
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {e.printStackTrace();
}
Then the following error is given;
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: rep\rp.jrxml (The system cannot find the path specified)
How I can keep jrxml files inside my NetBeans project and use jrxml files inside the project?
回答1:
This code works for me:
public class TestJasper {
public static void main(String[] args) {
try {
String reportSource = "resources/report1.jrxml";
Map<String, Object> params = new HashMap<String, Object>();
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
My project structure:
build
classes
TestJasper.class
dist
nbproject
resources
report1.jrxml
src
TestJasper.java
UPDATED:
For solving net.sf.jasperreports.engine.JRException: No report compiler set for language : null problem you can try to set groovy report language and add groovy-all-jdk14 library to classpath.
You can get groovy library here.
The sample of report header with language set to groovy:
<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"
...
language="groovy"
...>
回答2:
Here the solution.It is better to provide an absolute path in the jar file itself.Also when the jar is run,using *.jasper file is better to use instead of .jrxml it may cause for the speed of generating the report as well.Then that .jasper can be sent to JasperFillManager as a inputstream. That is it.
来源:https://stackoverflow.com/questions/7823756/jasperreports-class-path