Cannot load properties file from resources directory

JavaEclipseMavenMaven 3

Java Problem Overview


I imported a project from a Git repository and added Maven nature to it in Eclipse. In the resources folder, I added a configuration file called myconf.properties. Now, whenever I try to open this file from my Java code, I get FileNotFoundException. The file is also present in the target/classes folder generated after maven compiles the project.

Can anyone tell me what can be the problem? My Java code that tries to load this file is:

props.load(new FileInputStream("myconf.properties"));

where props is a Properties object.

Can anyone give me some hints on how to solve this issue?

Java Solutions


Solution 1 - Java

If the file is placed under target/classes after compiling, then it is already in a directory that is part of the build path. The directory src/main/resources is the Maven default directory for such resources, and it is automatically placed to the build path by the Eclipse Maven plugin (M2E). So, there is no need to move your properties file.

The other topic is, how to retrieve such resources. Resources in the build path are automatically in the class path of the running Java program. Considering this, you should always load such resources with a class loader. Example code:

String resourceName = "myconf.properties"; // could also be a constant
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties props = new Properties();
try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
    props.load(resourceStream);
}
// use props here ...

Solution 2 - Java

I think you need to put it under src/main/resources and load it as follows:

props.load(new FileInputStream("src/main/resources/myconf.properties"));

The way you are trying to load it will first check in base folder of your project. If it is in target/classes and you want to load it from there do the following:

props.load(new FileInputStream("target/classes/myconf.properties"));

Solution 3 - Java

If it is simple application then getSystemResourceAsStream can also be used.

try (InputStream inputStream = ClassLoader.getSystemResourceAsStream("config.properties"))..

Solution 4 - Java

Right click the Resources folder and select Build Path > Add to Build Path

Solution 5 - Java

Using ClassLoader.getSystemClassLoader()

Sample code :

Properties prop = new Properties();
InputStream input = null;
try {
    input = ClassLoader.getSystemClassLoader().getResourceAsStream("conf.properties");
    prop.load(input);

} catch (IOException io) {
    io.printStackTrace();
}

Solution 6 - Java

I use something like this to load properties file.

		final ResourceBundle bundle = ResourceBundle
				.getBundle("properties/errormessages");

		for (final Enumeration<String> keys = bundle.getKeys(); keys
				.hasMoreElements();) {
			final String key = keys.nextElement();
			final String value = bundle.getString(key);
			prop.put(key, value);
		}

Solution 7 - Java

I believe running from Eclipse, if you're using "myconf.properties" as the relative path, You file structure should look somehting like this

ProjectRoot
         src
         bin
         myconf.properties

Eclipse will look for the the file in the project root dir if no other dirs are specified in the file path

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionartaxerxeView Question on Stackoverflow
Solution 1 - JavaSeelenvirtuoseView Answer on Stackoverflow
Solution 2 - Javadev2dView Answer on Stackoverflow
Solution 3 - JavarnsView Answer on Stackoverflow
Solution 4 - JavaKevin BowersoxView Answer on Stackoverflow
Solution 5 - JavaDeep NirmalView Answer on Stackoverflow
Solution 6 - JavaKarthik PrasadView Answer on Stackoverflow
Solution 7 - JavaPaul SamsothaView Answer on Stackoverflow