I have a Java project generating jasper reports, what I do is I give path of a file (report) and run my project it runs fine, but when I have to give JAR file to my friend, it always need that file to be placed in that specific folder according to which my code get's the file path and then have to place file there, my code is below to give some idea
public void generateReport(String dateStart, String dateEnd) {
InputStream stream = null;
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/app", "root", "");
stream = new FileInputStream(new File("").getAbsolutePath() + "/reports/logReport.jrxml");
JasperDesign jasperDesign = JRXmlLoader.load(stream);
jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, connection);
//frame.getContentPane().add(dateSetter, BorderLayout.PAGE_START);
frame.getContentPane().add(new JRViewer300(jasperPrint), BorderLayout.CENTER);
frame.setResizable(false);
frame.setSize(900, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
isVisible = true;
connection.close();
stream.close();
}
within code it get the file from the path as given
/reports/logReport.jrxml
but when I put JAR file somewhere else and run it, it gives error that
C:\Desktop\reports\logReport.jrxml cannot find path specified.
I know it's due to code part new File("").getAbsolutePath() but I need to know how to make it so that it always get the file path from within project! So that I don't always have to place that particular file there!
reportsfolder located in your machine and in your friends machine? and will it be always at same location?getClass().getClassLoader().getResourceAsStream("file name");.