Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jasper Report and JAR file

 
Talhah Mafawalla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,

I am having a problem that i have been working on for about a week now. I a have just started using jasper reports.

The problem is that when i am runing my application in Netbeans 5.5 IDE i have no problems providing the path to the .jrxml file which is

Though when the application is complied into a jar file with the .jrxml file inside the jar file aswell, i get the following error:

I have tried to give the path dynamically using the following code:

but I also get an error. I would like to know how to give a reference to a .jrxml file located inside a jar file.

Tanks in advance.

Talhah Mafawalla
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if its compiled and packaged into a jar file your code should be pointing to the compiled report file (*.jasper) and not the raw version (*.jrxml).

hope this helps.
 
Paul Clapham
Sheriff
Pie
Posts: 20959
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Provide the absolute path within the jar file, not a path that's relative to something. For example if the "reports" directory is in the root of the jar file, then like this:And don't do getPath() after that. You can't get a File object from a jar file, so if you planned to pass a File to some method, try again. Find the version of the method that accepts an InputStream instead and call getResourceAsStream() instead of getResource().
 
Talhah Mafawalla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dennis and Paul,

I have found the solution to this problem by using the method suggested by Dennis, I have found that the JasperFillManager class has a method ".fillReport()" that excepts and InputStream as a paremeter, and I am happy to say that i have the report generated.

Thanks again for your input.

Talhah Mafawalla.
 
rama krishnan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which way the Jasper Report Tool is working in Java?
Is there any methods?
 
prabhu ganapathylingam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am using the following code

try
{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dgimageapp","root","ikure");
//Getting the xml file
InputStream in =getClass().getResourceAsStream("reports/report1.jrxml");

//
JasperDesign jasperDesign = JRXmlLoader.load(in);

//Build a new query
query = "select pid,fname,lname,mobileno from dg_patient where date between '"+from+"' and '"+to+"'";

// update the data query
JRDesignQuery newQuery = new JRDesignQuery();
newQuery.setText(query);
jasperDesign.setQuery(newQuery);
System.out.println("Done!");

//compiling and creating the report
//JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

//filling the Report and add it to
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);

// JasperViewer.viewReport(jasperPrint);
JRViewer viewer1=new JRViewer(jasperPrint);
JFrame jf=new JFrame();
jf.add(viewer1);
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

con.close();
}
catch (JRException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}

while i am running the code in netbeans IDE. it will show the report.
but when i am trying to run a jar, the report is not coming

help me

thanks in advance
 
marko zorko
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm developing app in Rational and this method is working locally... Creates pdf and everything... But when I compile my app and create jar with .jasper files and try to run my app over .bat it's not woriking anymore... like .jasper file is not found...

My method:
public void writePdfFile(List<MyObject> data, Map<String,Object> map, String FilePathAndNAme) throws IOException, JRException{

InputStream is = getClass().getResourceAsStream("reports/my_name.jasper"); //like in my jar

String fileType = ".pdf";
String outFileName = FilePathAndNAme+this.yearmm+ fileType;
JRDataSource dataSource = new JRBeanCollectionDataSource(data);

try{
JasperPrint jasperPrint = JasperFillManager.fillReport(is, map, dataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, outFileName);
}
catch (JRException jre){
jre.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
}

Error:
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: file:\C:\myJar.jar!\reports\my_name.jasper
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:77)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:536)
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdfFile(JasperRunManager.java:351)
at si.triglav.batch.porocilaKnjigovodstvo.myClass.writePdfFile(myClass.java:142)
at si.triglav.batch.porocilaKnjigovodstvo.myClass.executeBatch(myClass.java:88)
at si.triglav.batch.BatchRoot.initialization(BatchRoot.java:93)
at si.triglav.batch.porocilaKnjigovodstvo.myClass.main(myClass.java:43)
Caused by: java.io.FileNotFoundException: file:\C:\myJar.jar!\reports\my_name.jasper
... 7 more
 
Andrej Nadilo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,

I have the same problem as Talhah:

When running my application through IDE, the report generates without any problems - I am able to view it through JasperViewer, print it and everything seems to work fine.

However, when application is deployed to Jar the report doesn't get generated. All needed Jasper libraries are included in the Jar. The .jasper file is located inside the Jar but I have the same problem no matter where I locate it.

I tried starting the Jar through Command Prompt and got an error thrown while generating the report:

java.io.IOException: Stream closed .....

The code for generating the report is below.

I tried so many different things and nothing seems to work. What do you think the problem is? Please HELP

Code that generates a report:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic