[英]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. 我在尝试检索位于项目根目录之外的config.properties文件时遇到以下问题。
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). 从Main类内部的main()方法中,我必须访问config.properties文件,该文件位于项目根文件夹的同一级别(并且不在项目类路径中)。
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 我必须使用相对路径,所以我必须执行以下操作:从项目内的文件夹开始,我必须返回直到容器目录,然后在此处获取config.properties文件。
How can I do it? 我该怎么做? (I am under Windows environment)
(我在Windows环境下)
I think, in your situation, it's best to use the Classloader your Class provides. 我认为,在您的情况下,最好使用您的Class提供的Classloader 。
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. 唯一的问题是,您不能在像main这样的静态方法中编译该代码。 If you want to run it in a static method, you should change it to:
如果要以静态方法运行它,则应将其更改为:
Main.class.getClassLoader().getResources("config.properties");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.