简体   繁体   中英

How can I access to a .properties file situated outside the project folder and outside the project classpath?

I have the following problem trying to retrieve a config.properties file situated outside my project root directory.

So I have a situation like this:

CONTAINER DIRECTORY
     |
     |
     |------> config.properties
     |
     |
     |------>PROJECT-ROOT
                 |
                 |
                 |
                 |------> src
                     |
                     |
                     |------> mainPkg (the package name)
                                 |
                                 |
                                 |------> Main (the main class containing the main() method)

From the main() method situated inside the Main class I have to access to the config.properties file that is at the same level of the project root folder (and that is out of the project classpath).

I have to use relative path so I have to do something like this: starting from a folder inside the project I have to come back untill the CONTAINER DIRECTORY and here take the config.properties file

How can I do it? (I am under Windows environment)

I think, in your situation, it's best to use the Classloader your Class provides.

It should work like this:

getClass().getClassLoader().getResources("config.properties");

The only problem is, you can't compile that code in a static method like main. If you want to run it in a static method, you should change it to:

Main.class.getClassLoader().getResources("config.properties");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM