javator Posted March 18, 2009 Share Posted March 18, 2009 Hello, I'm working with netbeans on a Java Desktop Application, in this application I implemented JasperReports, which are created and compiled with iReport. In the java classes I call directly the compiled jasper file like in code below.the problem is: When I'm working on the IDE, the report running works out-of-the box , but once I make a build for the application, I couldn't start it anymore ( I got this : java.io.FileNotFoundException), I releazied that if I put the folder where the jasper file exist (called "report" in this case) in the same location where the JAR-File is, so it works normally!Knowing that in my Netbeans Project, the "report " folder is inside "src". Is there any way to set up the path to the jasper files , so that they'd be runnable directly from the builded JAR-File?I'm waiting forward to getting your help.Thanks in advance Code:private void printReport(Clients client) throws IOException, InterruptedException { JasperPrint jasperPrint; HashMap<String, String> parameter = setParameter(client); try { jasperPrint = JasperFillManager.fillReport("src\report\client.jasper", parameter, new JREmptyDataSource()); JasperExportManager.exportReportToPdfFile(jasperPrint, "ReportKunde.pdf"); Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler ReportClient.pdf"); p.waitFor(); } catch (JRException jre) { jre.printStackTrace(); } } Link to comment Share on other sites More sharing options...
javator Posted March 18, 2009 Author Share Posted March 18, 2009 javatorWrote: Hello, I'm working with netbeans on a Java Desktop Application, in this application I implemented JasperReports, which are created and compiled with iReport. In the java classes I call directly the compiled jasper file like in code below. the problem is: When I'm working on the IDE, the report running works out-of-the box , but once I make a build for the application, I couldn't start it anymore ( I got this : java.io.FileNotFoundException), I releazied that if I put the folder where the jasper file exist (called "report" in this case) in the same location where the JAR-File is, so it works normally! Knowing that in my Netbeans Project, the "report " folder is inside "src". Is there any way to set up the path to the jasper files , so that they'd be runnable directly from the builded JAR-File? I'm waiting forward to getting your help. Thanks in advance Code: I used double back-slash to give path to the jasper file ! Link to comment Share on other sites More sharing options...
gordonb Posted March 19, 2009 Share Posted March 19, 2009 I created a directory on the server, but not in the context of the application, e.g. E:Jasper.I then put all report files in this directory and reference it by an absolute path. This way a reportdesigner can develop reports and post them for use easily. Link to comment Share on other sites More sharing options...
javator Posted March 20, 2009 Author Share Posted March 20, 2009 Thanks for the answer, but what I'd like to do is to distribute a build of my Java application, as the JAR-File would be "stand-alone", that means that once the user run it, he'd will be also able to run and view the report on his local computer without any problem concernig the pointing path. I hope you'll figure out what I wanted to say (I don't really master english!). Thanks again Link to comment Share on other sites More sharing options...
gordonb Posted March 20, 2009 Share Posted March 20, 2009 To do this, you can put the .jasper files in a source directory in your app, and reference them via their relative path. Try a few examples creating a file with various relative paths to see how to do this. (Files with a filename but no path tend to end up in the domain1/config folder; work backwards from that location.) Link to comment Share on other sites More sharing options...
munga Posted March 21, 2009 Share Posted March 21, 2009 All of my reports are within a jar file, I load them via getSystemResourceAsStream like this.JasperPrint jp = JasperFillManager.fillReport( ClassLoader.getSystemResourceAsStream("com/foo/blah/reports/myreport.jasper"),params, jrrsds);hope it helpsmunga Link to comment Share on other sites More sharing options...
javator Posted March 23, 2009 Author Share Posted March 23, 2009 It works out of the box, thank you very very much munga ! I will use your proposed solution for the moment to distribute the runnable JAR-File alternatively to compiling through ANT-Task, till I'd learn how the Ant-tasks for JasperReports works, then I will try to accomplish it through Ant.Thanks anyway for everyone who has answered me on this thread.Best regards to JasperReports Developping team Link to comment Share on other sites More sharing options...
munga Posted March 24, 2009 Share Posted March 24, 2009 Here is what I use inside my ant compile target. <taskdef name="jrc" classname="net.sf.jasperreports.ant.JRAntCompileTask"> <classpath refid="classpath"/> </taskdef> <jrc srcdir="${src}" destdir="${build}" includes="com/**/*.jrxml"> <classpath refid="classpath"/> </jrc> hope it helpsmunga Link to comment Share on other sites More sharing options...
javator Posted March 24, 2009 Author Share Posted March 24, 2009 Hi munga,thanks again for your answer, I'll try once I'm finished with doing another task with JasperReport, which is the following:I used in the beginning to export JasperReport into a PDF-File through the method:JasperExportManager.exportReportToPdfFile(jasperPrint, "pdfFileName.pdf");Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler pdfFileName.pdf");p.waitFor();Which exports (saving then running) the generated report File into PDF Format, but what I 'd like to make is to generate and run the report without saving it to a file, knowing that I'm working on a java desktop application and not a Servlet.I noticed that it exists this method called: JasperRunManager.runReportToPdf()but I couldn't really realize it in my case. Any idea? Thanks in advane.Regards Link to comment Share on other sites More sharing options...
munga Posted March 24, 2009 Share Posted March 24, 2009 Hi Javator,Do you simply want to generate and view your report without creating a file?If that is the case its easy to do with the jasperViewer JasperPrint jp = JasperFillManager.fillReport(jr, params, jrtmds); JasperViewer jv = new JasperViewer(jp, false); jv.setVisible(true);from the viewer you can still then save a pdf file or print your report.munga Link to comment Share on other sites More sharing options...
javator Posted March 24, 2009 Author Share Posted March 24, 2009 Hi munga,yes exactly, i want to generate and view the report without creating a file. And I used before the method with the JasperViewer, but it seemed to be too slow when I click on the save icon of the JasperViewer Windows, and it took fast a minute in order to get the save dialog, it's why I thought to use another method, which allows to generate an run the report directly into PDF Format: So once I call the report, it would will be generated then willl Acrobat Reader open within the generated report for example and also without creating a file on the directory. Is it possible ?Thanks Link to comment Share on other sites More sharing options...
munga Posted March 24, 2009 Share Posted March 24, 2009 javator,I don't know how to generate a pdf report in memory and have Acrobat reader display it. You may have to look into the Acrobat Reader api for that.I do have a suggestion for your problem in saving the pdf file, via the JRViewer, taking a long time. I create a new thread to view the report so when saving a file in pdf or xls etc it does not tie up your GUI waiting for an I/O block to return.On another note if you use native calls in java you loose the run anywhere ability of java.hope this helpsmunga Link to comment Share on other sites More sharing options...
javator Posted March 24, 2009 Author Share Posted March 24, 2009 Hi Munga,I think I'll give up and choose between the two solutions, either the exportReportToPdfFile or the JRViewer. I know that Java is quite slow to call and to be called by native applications, maybe the JRViewer cannot be adapted so that the Save-Dialog appear more quicker.Anyway I'm very thankful to you by helping me through this thread.Best greetings Link to comment Share on other sites More sharing options...
prabhug1987 Posted October 28, 2010 Share Posted October 28, 2010 Hi I am using the follow code from=sdf.format(date1.getDate()).toString(); to=sdf.format(date2.getDate()).toString(); 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(); } for viewing the report. it works in the net beans IDE. but while i am trying to run jar it is not running. help methanks in advance. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now