0

I am trying to get the directory path of the properties file which is located inside the resource directory using the below code.

public String getFilePath(String fileName){
    File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
    return  file.getParent()+"/";
}

My setup will have 3 modules. Module1 has 2 & 3 as a dependency in maven. each module has its properties file inside the root resource directory - src/main/resources/app.properties

Now if I try to load the path of config.properties from module 2 singleton class using above method, I get the path of module3 properties file. How to get the path of the 2nd module's properties file.

Thanks in advance.

UPDATE The answer given in the duplicate question is, to take all resource files and check for the specific string in the path exist or not. I would like to directly get the resource file from the root directory which must be closer to the given class reference.

iDroid
  • 1,140
  • 1
  • 13
  • 30
  • Does this answer your question? [Read resources from specific JAR file (duplicate paths to file)](https://stackoverflow.com/questions/48892639/read-resources-from-specific-jar-file-duplicate-paths-to-file) – dnault Jul 12 '20 at 19:36
  • After raising the question, I came across https://stackoverflow.com/questions/6730580/how-to-read-several-resource-files-with-the-same-name-from-different-jars, which also takes a list of resources and iterates them. I added the contains part. But I am still curious to know if there any other way to reach the resource root directory located closer to the current class. – iDroid Jul 12 '20 at 20:53

0 Answers0