Unable to Call jasper report from Jar file - java

I am using the following code to call a jasper report
String reportSource = "/report.jrxml";
InputStream is = getClass().getResourceAsStream(reportSource);
JasperReport jasperReport = (JasperReport) JasperCompileManager.compileReport(is);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);
JasperViewer.viewReport(jasperPrint);
Code is working fine when i run it in netbeans ide. but when i build the application and create the jar and run it , i am not getting report popup.

Don't convert the results of getClass().getResource("report.jrxml") to a String, instead, you want to use getClass().getResourceAsStream("report.jrxml") and pass this into JasperCompileManager.compileReport(InputStream)
try (InputStream is = getClass().getResourceAsStream("report.jrxml")) {
JasperReport jr = JasperCompileManager.compileReport(is);
}
Having said all that, you should not be deploying or compiling your .jrxml files at runtime.
As part of your build process, you should be compiling the .jrxml files into .japser files and simply loading and filling them at runtime.
try (InputStream is = getClass().getResourceAsStream("report.jrxml")) {
JasperReport report = (JasperReport)JRLoader.loadObject(is);
}
This way you save yourself the hassle of wasting runtime compiling the report each time...
ps, you can also use...
JasperReport report = (JasperReport)JRLoader.loadObject(getClass().getResource("report.jrxml"));

Related

Loading a precompiled jasper file throws an exception: Class not found when loading object from file

Using JasperReports 6.11.0 in Eclipse, I precompiled a .jrxml file into a .jasper file. My problem is when I use the jasper file to load as a report, it throws an exception net.sf.jasperreports.engine.JRException: Class not found when loading object from file. This is the code I am using:
JasperReport jasperReport = (JasperReport) JRLoader
.loadObjectFromFile("src/main/resources/Report.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
Note that I tried using a template and still throws the same exception. Also, when I compile the jrxml in the Java code, it works perfectly.
InputStream input = new ClassPathResource("jasper.jrxml").getInputStream();
JasperDesign jasperDesign = JRXmlLoader.load(input);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());

How to use JRLoader.loadObjectFromFile() to generate a JasperReport

I'm trying to generate a jasper report. It worked in my Netbeans ide but after compiling it doesn't work and doesn't show an error. After reading this Jasper report is not working after making jar file I changed my code to this.
InputStream path = getClass().getResourceAsStream("\\my_package\\ChartOfAccounts.jasper");
HashMap param = new HashMap();
String cpname = cmpName();
String cpadd = cmpAdd();
param.put("CompanyName",cpname);
param.put("Address", cpadd);
JasperReport jr = (JasperReport)JRLoader.loadObjectFromFile(path);
JasperPrint jp = JasperFillManager.fillReport(jr, param, conn);
JasperViewer jv = new JasperViewer (jp,false);
jv.setTitle("Chart of Accounts");
jv.setVisible(true);
but when i use
JasperReport jr = (JasperReport)JRLoader.loadObjectFromFile(path);
it says I should use a string type variable there. what should I do?
According to http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/util/JRLoader.html
there are several methods for loading objects. JRLoader.loadObjectFromFile takes String as an argument and in the code above an InputStream is passed which causes the error.
Based on the use case you should consider which method to call.
If you need to use InputStream you should use JRLoader.loadObject(InputStream)
If you need to read from a file (which is external to the program and does not live on the classpath) you should use JRLoader.loadObjectFromFile

Jasper report is not working after making jar file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have written the following code for creating jasper report, this code working fine in NetBeans IDE, but after creating jar file of that project the report is not opening. Its also not showing any error.
What can be the issue?
Code for creating jasper report
//Path to your .jasper file in your package
String reportSource = "src/report/Allvendor_personal_info.jrxml";
try
{
jasperReport = (JasperReport)
JasperCompileManager.compileReport(reportSource);
jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
//view report to UI
JasperViewer.viewReport(jasperPrint, false);
con.close();
}
catch(Exception e)
{
JOptionPane.showMessaxgeDialog(null, "Error in genrating report");
}
The path src will not exist at runtime and you should never reference it.
Based on this, the Allvendor_personal_info.jrxml will be an embedded resource, stored within the Jar file, you won't be able to access it like you do normal files, instead, you need to use Class#getResource or Class#getResourceAsStream
String reportSource = "/report/Allvendor_personal_info.jrxml";
InputStream is = null;
try
{
is = getClass().getResourceAsStream(reportSource);
jasperReport = (JasperReport)JasperCompileManager.compileReport(is);
jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
//...
} finally {
try {
is.close();
} catch (Exception exp) {
}
}
Now, having said that, there should be very little reason to ever compile a .jrxml file at runtime, instead, you should compile these files at build time and deploy the .jasper files instead. This will improve the performance of your application as the complication process is not short even for a basic report.
This would mean you would use...
jasperReport = (JasperReport) JRLoader.loadObjectFromFile(is);
Instead of JasperCompileManager.compileReport
Set environment variables to lib folder of jar files
Well guys, I don't know if It's too late, but I wasted 1 day searching the solutions to this problem, about JasperReport after building an Jar executable. To get your Reports working after building a jar, you just need to write the following lines
String reportUrl = "/reports/billCopyReport.jasper"; //path of your report source.
InputStream reportFile = null;
reportFile = getClass().getResourceAsStream(reportUrl);
Map data = new HashMap(); //In case your report need predefined parameters you'll need to fill this Map
JasperPrint print = JasperFillManager.fillReport(reportFile, data, conection);
JasperViewer Jviewer = new JasperViewer(print, false);
Jviewer.setVisible(true);
/* var conection is a Connection type to let JasperReport connecto to Database, in case you won't use DataBase as DataSource, you should create a EmptyDataSource var*/
you need to copy folder which has jasper/Jrxml files and put it same directory of your jar file.
whenever you write code like this
String reportSource = "/report/Allvendor_personal_info.jrxml";
//It will look for this file on your location so you need to copy your file on /report/ this location
InputStream is = null;
try
{
is = getClass().getResourceAsStream(reportSource);
jasperReport = (JasperReport)JasperCompileManager.compileReport(is);
jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
//...
} catch(Exception e){
}

Getting 'File Not Found Exception' with JasperReports on Java Web Application

I am creating Jasper reports from My Java Web Application through a pre-designed Jrxml file. the file is in My web folder (Netbeans) in a directory named jrxml so I am trying to get at it using this Method.
public void generateChurchReport(IncomeExpenseBean ieb) {
church = ieb.getChurch();
user = ieb.getUser();
String currdate = dt.getCurrentDate();
Connection conn = db.getDbConnection();
Map parameters = new HashMap();
try{
parameters.put("ChurchName", church);
JasperReport jasperReport = JasperCompileManager.compileReport("/jrxml/ChurchIncome_expenseReport.jrxml");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
File f = new File(user + church+ currdate + ".pdf");
JasperExportManager.exportReportToPdfFile(jasperPrint, f.getAbsolutePath());
Desktop.getDesktop().open(new File(f.getAbsolutePath()));
}catch(Exception asd){
System.out.println(asd.getMessage());
}
}
I am getting File Not Found Exception because the Application is expecting the file somewhere in ;
C:\Program Files\glassfish-3.1.2.2\glassfish\domains\TestDom\jrxml\
How do i read this file in my web folder and How can I create the Reports Inside the same folder?
EDIT If I do not give Any Paths My reports are getting Generated at C:\Program Files\glassfish-3.1.2.2\glassfish\domains\TestDom\ if the jrxml file is in that Location.
What you are doing wrong here is, you are trying to locate your jrxml file somewhere in web folder from your java class. This will definitely raise "File Not found error" at run time because of incorrect context path. You could simply do the following:-
Make a folder named say "Jrxml" under your java classes package. Suppose java classes package is com.ejb.beans, make a folder com.ejb.beans.jrxml.
Put all your jrxml files into this folder.
In your java class, load the class loader and locate the jrxml by its name and you will easily access it. Here is the code:-
ClassLoader classLoader = getClass().getClassLoader();
InputStream url = null;
url = classLoader.getResourceAsStream("Report.jrxml");
This url can be used to compile report as :-
JasperReport jasperReport = JasperCompileManager.compileReport(url);
To create the report output file, you could store it at some path in your application server. Set your server path in environment variable and extract it in your class at runtime like this :-
String serverHomeDir = System.getProperty("server.home.dir");
String reportDestination = serverHomeDir + "/domains/ReportOutput/report.html";
// now print report at reportDestination
JasperExportManager.exportReportToHtmlFile(jasperPrint, reportDestination);
Your html file will be generated at the required destination, which you can easily read and render it, the way you want to, through your web page.

JasperReports class path

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?
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"
...>
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.

Categories

Resources