0

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!

4
  • Where is reports folder located in your machine and in your friends machine? and will it be always at same location? Commented May 13, 2013 at 19:23
  • reports folder is inside the project, now I don't get why it doesn't get the path from that project? or what I have to do so that it always pick that path as it picks path for all other reports placed in that folder Commented May 16, 2013 at 9:36
  • 1
    First of all you should be using relative path to get hold of that file. Second as you are making jars you should be using getClass().getClassLoader().getResourceAsStream("file name");. Commented May 16, 2013 at 15:48
  • the file name is supposed to be only file name or complete path? yet I followed and getting this error "java.util.MalFormedUrlException" Commented May 18, 2013 at 17:08

2 Answers 2

2

If you want to make it an absolute path to the same directory always you just specify the entire path.

stream = new FileInputStream(new File("C:/path/to/my/reports/logReport.jrxml"));

If you want it relative to the directory where your jar is running.

stream = new FileInputStream(new File("path/to/my/reports/logReport.jrxml"));

If instead you are looking to load the file from your Jar itself, you need to do it differently.

stream = this.getClass().getResourceAsStream.("/path/to/my/reports/logReport.jrxml");
Sign up to request clarification or add additional context in comments.

9 Comments

but what when i send that JAR to someone and it picks all files which are there in SRC folder but not the one in folder I created?
Wait, so you want the files from within your own jar? or a file on the computer?
no no I meant to say that I have created a new Report file (logReport) and I just want this file to be picked as others are, without any change of path each time I send this jar file to someone or place JAR file to some other folder
and yes I mean to pick that file from my JAR file not from specific location from my computer :)
Revised the answer, you need to use getResourceAsStream
|
0

Better you read the file form a specified location

Here is the link to read the file from a specified location.

but if the same code is running on a server you have to specify the path relative to the root directory where the server is running

 String location = "/opt/reports/";
 String filename = "logReport.jrxml";

 File file = new File(location + filename);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.