How I can join compiled jasper file and my project in java? - java

I have design my project in iReport and I have compiled successfully. Now I want to add the compiled jasper file to add in my java project. May any one help me?

Just add them in some package in your source tree. Your build process or IDE should copy them in your classes directory or in the generated jar file, along with your classes. The reports may then be loaded with Class.getResource() or Class.getResourceAsStream().

Here is the code you need in order to get two values from two combo-boxes, add them to a HashMap and pass the map to iReport. These paramaters, for this example "storeName" and "actionCode", are used to specify values for the query which is stored inside the iReport. The report jasper file is "../ireps/AccessCounter.jrxml". The viewer is a JDialog. If you have no parameters to pass to your report just skip the map.put. con is the connection to the database.
Make sure that you include the necessary jar file to your path.
try {
String shopName = jComboBox1.getSelectedItem().toString();
String actionCode = jComboBox2.getSelectedItem().toString();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("storeName", shopName);
map.put("actionCode", actionCode);
URL reportFileURL = getClass().getResource("../ireps/AccessCounter.jrxml");
File reportFile = new File(reportFileURL.toURI());
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, con);
JasperViewer jv = new JasperViewer(jasperPrint);
JDialog viewer = new JDialog(this, "Batch Report", true);
viewer.setBounds(jv.getBounds());
viewer.getContentPane().add(jv.getContentPane());
viewer.setResizable(true);
viewer.setIconImage(jv.getIconImage());
viewer.setVisible(true);
} catch (JRException exc) {
System.out.println(exc.getMessage());
} catch (URISyntaxException exs) {
System.out.println(exs.getMessage());
}

Related

JasperReport File Not Found error in fat jar

I made a fat jar that contains JasperReport but when I try to execute the jar this pop's out in the cmd
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: file:\C:\Users\User\Desktop\test.jar!\fxml\Report.jrxml (The filename, directory name, or volume label syntax is incorrect)
the "!" makes the problem here I think, but I don't know why it is added to the path nor how to remove it.
Here is my code:
public class PrintReport extends JFrame {
public void showReport(Connection conn) throws JRException {
String reportSrcFile = getClass().getResource("/fxml/Report.jrxml").getFile();
String reportsDir = getClass().getResource("/fxml/").getFile();
JasperReport jasperReport = JasperCompileManager.compileReport(reportSrcFile);
// Fields for resources path
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("reportsDirPath", reportsDir);
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
list.add(parameters);
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, conn);
JRViewer viewer = new JRViewer(print);
viewer.setOpaque(true);
viewer.setVisible(true);
this.add(viewer);
this.setSize(1000, 500);
this.setVisible(true);
}}
Resources are not accessible as File once you package them in a jar. Load the report from an InputStream instead of a file instead:
JasperReport jasperReport = JasperCompileManager.compileReport(getClass().getResourceAsStream("/fxml/Report.jrxml"));
As for the parameter: Try using resource paths, if it's used to resolve resources in your report file. JasperReports should be able to access resources via ClassLoader.

My Jasper file wont work

I'm trying to make this program but i cannot take a report out of it... even if it worked in java netbeans when i tried to run it using the executable JAR file i get this error !
my coding part is
JRTableModelDataSource datasource = new JRTableModelDataSource(dt.getModel());
String reportSource = "file/door_reports.JRXML";
Map<String, Object> params = new HashMap<>();
params.put("c1", subtot.getText());
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint1 = JasperFillManager.fillReport(jasperReport, params, datasource);
JasperViewer.viewReport(jasperPrint1, false);
error is : error compiling java source file D:/test/dist/Blank_A4_1426091490655_11644
above file do not even exist! anybody know what would be the reason?
Use this method, it should be useful for you:
public static void GenerateReport(String filename, HashMap parameter) {
try {
JasperDesign jspDesign = JRXmlLoader.load(filename);
JasperReport jspReport = JasperCompileManager.compileReport(jspDesign);
JasperPrint jspPrint = JasperFillManager.fillReport(jspReport, parameter, yourconnection);
JasperViewer.viewReport(jspPrint, false);
} catch (Exception e) {
JOptionPane.showConfirmDialog(null, e);
}
}
and be sure of the path of the file, good luck.
Did you rename your jxml after you edited it in Jasper?
The report sets the report file name you chose while creating it in Jasper as name, so if you did simple rename your file and not change the name directly in the jxml too... i think there could be your problem.

On trying to print JasperReports report using Java, the Tomcat Apache Server is shutting down when trying to view the report again

I am trying to use JasperReports to create reports in my project.
I already have the .jrxml file generated. After that I have created the front end (a form) and on clicking "Submit", I need to print the report. On Clicking "Submit" button, I am calling the below code in my Action class (Struts 1.2),
if (actionForm.get("hiddenAction").toString().equalsIgnoreCase("Submit")) {
try {
//DataBase
connection = ConnectionHelper.getUniqueInstance().getConnectionFromPool();
Map<String, Object> parameters = new HashMap<String, Object>();
String report = "E:\\Report\\RP_BI_SUCCESS.jrxml";
String fromStrDate = "01/12/2013";
String toStrDate = "25/12/2013";
parameters.put("I_SPMID", 0);
parameters.put("I_GATEWAYLKUPID", 0);
parameters.put("I_FROMDATE", Utilities.convertStringDateToSQLDate(fromStrDate));
parameters.put("I_TODATE", Utilities.convertStringDateToSQLDate(toStrDate));
parameters.put("I_MODE", "A");
try {
JasperReport jr = JasperCompileManager.compileReport(report);
JasperPrint jasperPrint = JasperFillManager.fillReport(jr, parameters, connection);
JasperViewer.viewReport(jasperPrint);
jasperPrint = null;
} catch (Throwable t) {
t.printStackTrace();
}
}
}
To set this up, I have used JasperReports 5.5.0 (executable JAR file) in a folder in my project.
Case 1:
When I click on "Submit" button, i am able to get the JasperViewer window with the necessary report i wanted. Then if i am closing the report the Tomcat Apache Server (Tomcat Apache V 7.0) is getting shut down automatically.
Case 2:
But if i am clicking on "Submit" again without closing the previous opened report, then its fine.
Can anyone please help me to solve this issue?
Try replacing
Jasper.viewReport(jasperPrint)
by
JasperViewer jasperViewer = new JasperViewer(jasperPrint, false, null);
jasperViewer.setVisible(true);
It works for me just fine in Tomcat, in Glassfish doesn't.

Jasper Report and Netbeans 7.2

I'm having trouble to load a report through a button.This is my code.When I click the button it shows the down image error.
try {
InputStream in =
getClass().getResourceAsStream("C:/Users/RaMiNdU/Documents/NetBeansProjects/TimeTable
Generator/src/timetable/generator/sem1.jrxml");
JasperDesign jasperDesign = JRXmlLoader.load(in);
Map<String, Object> param = new HashMap<>();
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param );
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
System.out.println(e.getMessage());
}
add commons-digester.jar to your classpath and you should be through
commons-digester is not available in the Classpath.
Any start-up script is overriding Classpath environment variable.
Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due
to failure of static initialization is quite common.
Your class may not be visible while using multiple class loaders.

How to show the content of a List in JasperReports

I have a Collection of type List that I want to pass it to a report using JasperReports and iReport on NetBeans, but I can't manage to show the content of the list on the Report.
I don't know how to show the data in the Details section of the jrxml. What parameter should I use?
The list has Objects of type Proveedor (Provider in Spanish) and has variables like name, code, etc.
Here is the code of the method who call to JasperReports:
public void generateReport(List<Proveedor> aList){
String reportName = System.getProperty("user.dir") + "\\src\\reports\\lista-proveedores.jrxml";
JRBeanCollectionDataSource dataSource;
JasperReport jasperReport;
JasperPrint jasperPrint;
try{
//1-I fill the dataSource with the Collection
dataSource = new JRBeanCollectionDataSource(aList);
//2-Compile the XML
jasperReport = JasperCompileManager.compileReport(reportName);
//3-Fill the report with the datasource
jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);
//4-Export to PDF and save to disk
JasperExportManager.exportReportToPdfFile(jasperPrint, "Proveedores.pdf");
System.out.println("Finished!");
}catch (Exception e){
System.out.println(e);
//e.printStackTrace();
}
}

Categories