My jrxml file creates an empty PDF file - java

I created an jrxml file using iReport Designer tool. It works nice in iReport Designer tool. Now I am trying to compile that jrxml file using a Java program. The MySQL query is there in the jrxml file. I think the jrxml file is okay since it works as expected in iReport Designer tool.
When I run my Java program to compile and generate a PDF file of a report with JREmptyDataSource, it generates a pdf without any data filled, but the graphics ans static texts are there in the generated pdf. I know there is no data since I used a JREmptyDataSource.
Then I created a connection to my database. If things are okay, it should generate the PDF file with relevant data. Isn't it?
This is my code
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_db", "root", "123");
System.out.println("INFO: Connected succesfully to the database!");
JasperReport jasperReport = JasperCompileManager.compileReport("J:/reports using iReport tools/installmentListForACustomer.jrxml");
// JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,new HashMap(), new JREmptyDataSource());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,new HashMap(), c);
JasperExportManager.exportReportToPdfFile(jasperPrint, "fromXml.pdf");
System.out.println("INFO: Success!");
} catch (Exception e) {
System.out.println("EXCEPTION: " + e + " \n");
e.printStackTrace();
}
But the issue is, even though it generates a pdf file, it contains nothing. Not even the static texts and graphics. it is complete blank.
Query string is in the jrxml file. Database connection is successful as well. I cannot figure out why it generates an empty PDF.
Could you please explain to me why doesn't it generate a pdf file with relevant in database?

There is a report configuration which may be an issue in your example:
When No Data
You can specifiy what your report should show when there is no data available. This could be one explanation (your query may don't work) and so nothing is displayed.
To check this you can try enter When no Data = No Data Section and add a "No Data" band in your report.

Related

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.

iReport integration with extjs4.0.2a and Java + Spring + Hibernate

I am working on extjs 4.0.2a along with Java. I am using some framework with java such as Hibernate/JPA and Spring. Now I want to use a Jasper Report designing tool i.e. iReport to generate my reports in different formats (.xls,.pdf) etc. I am familiar with iReport. I generated the iReport file in .jrxml and .jasper format using the jdbc connection.
Now i want to integrate my generated report with JAVA so I can get the report in either .pdf or .xls format. When I click on pdf icon I can download the report generated in .pdf format.
I am using Ext js 4.0.2a mvc architecture. Anyone having idea how to proceed can help me
My solution for this problem is simple. As you are using ExtJs I think your controller is in JavaScript.
Do a request for the java server to create your report in pdf, xls, whatever
Save the report in your session with a key(can be the time in mileseconds)
Return the key to the view and open a new window to ask the server for your report
The new windows should contain a URL like this http://localhost:8080/myApplication/report?key=312312313
If you want to download to xsl you can try this:
private void exportToExcel(HttpServletResponse resp, JasperPrint jasperPrint) throws IOException {
String reportfilename = tagreport("file") + "repor.xls";
JExcelApiExporter exporterXLS = new JExcelApiExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT,jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, resp.getOutputStream());
resp.setHeader("Content-Disposition", "inline;filename="+ reportfilename);
resp.setContentType("application/vnd.ms-excel");
try {
exporterXLS.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
}

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