2

I' m using JasperReports in my Java application.

I have a package named "reports" to store all the reports generated. Here is the way I'm calling my jasper report in my application.

JasperDesign jd  = JRXmlLoader.load("C:\\Users\\Sandaru Weerathunga\\Desktop\\Dasatha Institute\\src\\reports\\teacherPay.jrxml");

This is working.
Instead of giving the full path , I tried:

JasperDesign jd  = JRXmlLoader.load("/reports/teacherPay.jrxml");

But this is showing an Error while running the program:

net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: 
/reports/teacherPay.jrxml (The system cannot find the path specified)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:176)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:156)

It is not suitable to give the full path to the JRXmlLoader because if you are going to run this application in other computer you have to change all the coding according to the computer path. So help me on this

Alex K
  • 21,162
  • 18
  • 101
  • 217

2 Answers2

5

/reports/teacherPay.jrxml is an absolute file path, meaning, go to the root of the current drive and find the file teacherPay.jrxml in the reports directory...

Which, if I read your question correctly, isn't what you want

Instead, try loading the report as a resource (given the fact that you state that it's within a package

JasperDesign jd  = JRXmlLoader.load(getClass().getResource("/reports/teacherPay.jrxml"));

If the report isn't packaged within your application context, then you will need to use a relative path instead, for example.

JasperDesign jd  = JRXmlLoader.load("reports/teacherPay.jrxml");

Now, having said that. Unless you are making dynamic changes at runtime, you shouldn't be loading the jrxml file, but instead, should have pre-compiled the file and should be loading the .jasper file instead. This will be faster and generally less error prone...

MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
  • Thanks a lot for the help... JRXmlLoader.load(getClass().getResource("/reports/teacherPay.jrxml")) is not working but JRXmlLoader.load(getClass().getResourceAsStream("/reports/teacherPay.jrxml")) is working well.. anyway thank u again.. :) –  Dec 17 '13 at 05:38
  • Can never remember if it takes a `URL` or `InputStream` - Glad you got it working though – MadProgrammer Dec 17 '13 at 05:39
1
JasperDesign jd  = JRXmlLoader.load(getClass().getResource("/reports/teacherPay.jrxml"));

This is not working some time, because getResource() returns URL. If your file path contains " " it returns "%20" Like this

"C:\\Users\\Sandaru Weerathunga\\Desktop\\Dasatha Institute\\src\\reports\\teacherPay.jrxml"

returns

"C:\\Users\\Sandaru%20Weerathunga\\Desktop\\Dasatha%20Institute\\src\\reports\\teacherPay.jrxml"

In that matter you can use getResourceAsStream() method that retuns InputStream. Try this, This works for me.

JasperReport jp = JasperCompileManager.compileReport(getClass().getResourceAsStream("/reports/teacherPay.jrxml"));