I have an Eclipse project with the following directory structure:
MyProj
>> src/main/java
>> src/main/config
>> src/test/java
>> src/test/config
Inside src/test/config I have a properties file called app.properties full of properties that both the classes in src/main/java and src/test/java use.
All the code that I am using to look for the properties file looks like this:
try {
Properties props = new Properties();
props.load(new FileInputStream("app.properties"));
someString = props.getProperty("app.title.color");
// etc.
} catch(Exception exc) {
// Handle exception...
}
However when I run this I am getting the following error (handled inside the catch clause above):
java.io.FileNotFoundException:
app.properties (The system cannot find the file specified)
But when I move the app.properties file to my project root (at the same level as, say, src/main/java) and re-run the code, it works great.
Obviously, this is a classpath issue. If I go to Build Path >> Configure Build Path >> Source I see that src/test/config is in fact a source folder on the build path. Which may not mean its also on the class path!!!
How can I configure Eclipse to look for src/test/config on the class path so that I can place my properties file in there but still have it available at runtime (when I run unit tests)?
Thanks in advance!
The right way to do this (which will ensure your code continues to work should it ever be put into a JAR), is to add
src/test/configto the classpath of your project, and to load the resources usinggetResourceAsStream()method ofClassLoader. e.g.