Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot load properties file from resources directory

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?

like image 456
artaxerxe Avatar asked Dec 03 '13 10:12

artaxerxe


People also ask

How do I read the properties file from the resource folder?

In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass().

How read properties file from src main resources?

The files in src/main/resources are copied to the target/classes directory by Maven, and this directory is then copied to the WEB-INF/classes directory of the deployed web application, which is in the classpath of your webapp. So you just need to load the file using the class loader: properties. load(MyClass.

How do I view the resources folder in a jar file?

This works when running inside and outside of a Jar file. PathMatchingResourcePatternResolver r = new PathMatchingResourcePatternResolver(); Resource[] resources = r. getResources("/myfolder/*"); Then you can access the data using getInputStream and the filename from getFilename .


2 Answers

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 ... 
like image 100
Seelenvirtuose Avatar answered Sep 20 '22 03:09

Seelenvirtuose


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")); 
like image 23
dev2d Avatar answered Sep 23 '22 03:09

dev2d