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

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

Related

Unable to Call jasper report from Jar file

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"));

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){
}

Change path JRXML in Java

In My Java app, i have structure like this,
sip
build
dist
nbproject
src
sip
Form.java
schedule.jrxml
test
And this my code to load jrxml in Form.java
try{
//String schedule_all ="E:\\My Data\\Tugas Akhir\\repos\\sip\\src\\sip\\jadwal.jrxml";
InputStream file = this.getClass().getClassLoader().getResourceAsStream("/sip/schedule.jrxml");
JasperReport jr= JasperCompileManager.compileReport(file);
JasperPrint jp = JasperFillManager.fillReport(jr, null,con);
JasperViewer jv = new JasperViewer(jp,false);
jv.setVisible(true);
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error show Document");
tambah_log(e.toString());
}
I Want to replace this path(E:\My Data\Tugas Akhir\repos\sip\src\sip\jadwal.jrxml";)
to my local project path like this ("/sip/jadwal.jrxml").
but i got this error
net.sf.jasperreports.engine.JRException:
java.net.MalformedURLException
Any Idea? Because i want deploy my system to another computer. And i wont configure its path again.
FIXED
I change My Code like this.
InputStream file = this.getClass().getClassLoader().getResourceAsStream("/sip/jadwal.jrxml");
JasperReport jr= JasperCompileManager.compileReport(System.getProperties().getProperty("java.class.path").split(";")[System.getProperties().getProperty("java.class.path").split(";").length - 1]+"\\sip\\jadwal.jrxml");
JasperPrint jp = JasperFillManager.fillReport(jr, null,con);
JasperViewer jv = new JasperViewer(jp,false);
jv.setVisible(true);
Your getResourceAsStream("/sip/schedule.jrxml"); call doesn't accept that type of "URL"
Check the documentation, if its a local file it probably wants "file:///C:/path/to/whereever/sip/schedule.jrxml"
Or you could see the solution posted here: https://community.oracle.com/thread/620656?start=0&tstart=0
Or better yet here
http://forum.spring.io/forum/spring-projects/container/62863-how-to-use-getresourceasstream-to-get-a-file-stream
In windows operating systems java accepts the drive having the os installation as root directory. If you put your /sip directory to that drive, for example (and generally) to C drive. Then you can reach it as
this.getClass().getClassLoader().getResourceAsStream("/sip/schedule.jrxml"); .
Putting the schedule.jrxml to a package or to a resource folder of the project is a better practice though, because in this case you may install it anywhere you want.

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: Error loading object from file while compiling report through jsp code

I have created a master report and a sub report. I have written the code in jsp to call the master report but it gives an error.
Error Loading object from file: "path where the sub report resides".
Please help me out if I am doing something wrong.
jasperdesign = JasperManager.loadXmlDesign(getServletConfig()
.getServletContext().getRealPath("/reports/VoucherReport.jrxml"));
jasperReport = JasperCompileManager.compileReport(jasperdesign);
HashMap map = new HashMap();
map.put("myParam", voucherId);
jasperPrint = JasperFillManager.fillReport(jasperReport, map, con);
JasperManager.printReportToPdfFile(jasperPrint, getServletConfig()
.getServletContext().getRealPath("/reports/myReport.pdf"));
It is throwing an exception on jasper print. Please help me out. I need a solution for this voucher report is a master report, do I also need to compile subreport too.
i had also some problems with my web app and the path for subreports. Look at the *.jrxml file and check if it uses an absolute path for the subreport. For me it helped to send the RealPath of my application to the report with a parameter. This parameter is used for the location of the subreport.

Categories

Resources