JasperReport File Not Found error in fat jar - java

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.

Related

Spring Webflux: FileNotFoundException inside my JAR

I am exports PDF usgin jasper reports. In develop, it works fine. But when compile the jar, system throws and FileNotFound Exception for "src/main/resources/reports/myJasperReport.jxml"
When I explore the JAR,I came across that the URL for the report is "/BOOT-INF/classes/resports/myJasperReport.jxml"
I found this link to File inside jar is not visible but didn't solve my problem.
Could you help me please?
#Slf4j
#Service
public class ReportService {
private static final String REPORTS_BASE_PATH = "src/main/resources/reports/";
public ByteArrayInputStream exportReport(
String reportFileName,
Integer idCC,
Map<String,Object> parameters
) throws Exception {
Connection connection = CustomEntityManagerFactoryPostgresImpl
.getInstance()
.getConnection(idCC);
File file = ResourceUtils.getFile(REPORTS_BASE_PATH + reportFileName);
JasperReport jasperReport = JasperCompileManager.compileReport(file.getAbsolutePath());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
return new ByteArrayInputStream(outputStream.toByteArray());
}
}
Add classpath: prefix to your path. And consider the resources folder to be a root.
ResourceUtils.getFile("classpath:reports/" + reportFileName);
UPD:
Actually, you don't need to get a file at all. Try to get resource as stream:
InputStream report = ReportService.class.getClassLoader().getResourceAsStream("reports/" + reportFileName);
JasperReport jasperReport = JasperCompileManager.compileReport(report);

How to print Jasper reports from servlets?

I am trying to print a Jasper report from my web application's servlet. Below is the code which calls the printing function.
//Print the report
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("companyName",userBean.getCompanyName().trim());
params.put("companyPhone",userBean.getPhone().trim());
params.put("patientName",demographicsBean.get(0).getfName()+" "+demographicsBean.get(0).getmName()+" "+demographicsBean.get(0).getlName());
params.put("patientAge",String.valueOf(demographicsBean.get(0).getDob()));
params.put("address",demographicsBean.get(0).getAdrLine1()+" "+demographicsBean.get(0).getAdrLine2());
java.util.Date date = new java.util.Date();
params.put("date",String.valueOf(new Date(Calendar.getInstance().getTimeInMillis())));
params.put("doctorName",subUserBean.getFirstName()+" "+subUserBean.getLastName());
params.put("drugName",drug);
params.put("ptInstructions",ptInstructionsTxt);
params.put("quantity",request.getParameter("quantityTxt"));
params.put("refills",refilTxt);
Connection connection = DBMaster.getInstance().getConnection();
ReportConnector r = new ReportImpl();
r.printReport(getClass().getResourceAsStream("/ReportResources/Prescription2.jasper"), params,connection);
Below is the code printing the report.
public void printReport(InputStream path, Map<String, Object> params, Connection con) throws JRException
{
JasperPrint jasperPrint = JasperFillManager.fillReport(path, params, con);
JasperPrintManager.printReport(jasperPrint, false);
}
The issue is, eventhough this worked in localhost, nothing is getting printed (I do not have a printer now, but MS One Note should be open as the printer. That is happening in localhost).
what is wrong here?

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.

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 I can join compiled jasper file and my project in 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());
}

Categories