1

I have some jasper reports file inside a directory called "reports" which is located 2-3 level at the bottom of the "src" package in eclipse IDE. How could get the related file paths inside "reports" folder. I don't want to get the paths like "C:\Users\Kara\workspace\MyProject\src\main\resources\reports\someFile.jrxml". I tried to get them by following code but it didn't give me the real path.

File testFile = new File("someFile.jrxml");
String absPath = testFile.getAbsolutePath();
0

2 Answers 2

2

If the reports are alway in your classpath (which they appear to be, assuming a standard Maven directory layout), use Class.getResource()/ClassLoader.getResource() to get a URL pointing to your report file. You can use this URL to extract the absolute path to your report file.

URL reportUrl = getClass().getResource("reports/someFile.jrxml");
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this:

URL url = getClass().getResource("/reports/someFile.jrxml");
url.getPath();

3 Comments

I think it should be "reports/someFile.jrxml" instead of "/reports/someFile.jrxml".
Thanks for the reply. Where does the "getClass()" method point to? I tried this code from inside a Test.java class but it didn't work, got Null Pointer Exception. URL url = Test.class.getResource("/reports/someFile.jrxml");
In my local computer getClass().getResource(file path) works fine and I am able to pass ".jrxml" file path and generate reports. However, when build the project and deploy it in remote server the specified file cannot be found. I encounter "java.io.FileNotFoundException". Thanks alot for the consideration.

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.