Alex
Alex

Reputation: 7511

Finding a file in a root directory of jar exported from Eclipse for JUNIT testing

In my Eclipse JUNIT test I have several text input files that are opened by

new BufferedReader(new FileReader(inputFileName));.  

They are placed in the root directory of the project After exporting this project to jar these files are placed to the jar root directory.
I run the JUNIT test from LINUX as

java -cp "<my jar>:junit-4.8.2.jar:jaxb-impl.jar" junit.textui.TestRunner <test class name>

However, in this case I am getting java.io.FileNotFoundException What is the best way to fix this problem on LINUX and at the same time still able to run the tests from Eclipse?

Upvotes: 1

Views: 775

Answers (3)

Alex
Alex

Reputation: 7511

What I want was achieved by adding * to the classpath and putting properties file into run directory. This feature started in Java 6. Then the properties file is achievable in classpath and is achieved both from Eclipse and command prompt running. It is a little different from my question – the properties file is not in jar, but that is what is needed. Plus, one can edit the properties file outside of Eclipse

Upvotes: 1

ceasaro
ceasaro

Reputation: 615

You could use the Class method getResourceAsStream

There's also an other issue on stackoverflow see: getResourceAsStream() vs FileInputStream

Upvotes: 0

hoaz
hoaz

Reputation: 10161

Use Class#getResourceAsStream() method to load classpath resources:

InputStream is = this.getClass().getResourceAsStream("myfile.txt");

You can convert it to Reader interface using InputStreamReader adapter.

Upvotes: 2

Related Questions