[英]How define relative path to file?
I have a project with cucumber and maven, work from Intellij IDEA.我有一个黄瓜和 maven 项目,来自 Intellij IDEA。
I need to use a relative file path.我需要使用相对文件路径。 Now I am working on mac os, but then the project will also be used on windows.现在我在 mac os 上工作,但是该项目也将在 windows 上使用。
Here is my RunnerTest.Java Class这是我的 RunnerTest.Java 类
public class RunnerTest {
public static void main(String[] args) throws Throwable {
System.setProperty("TagConfigFile", "../../config/application.properties");
//...
String sConfigFile = System.getProperty("TagConfigFile", "null");
try (InputStream streamFromResources = Props.class.getClassLoader().getResourceAsStream(sConfigFile)) {
/* work wiht streamFromResources */
} catch (IOException | NullPointerException ee) {
throw new PropsRuntimeException("Failed to access properties file", ee);
}
}
}
Here is the image of my project structure (for clarity, added highlighting):这是我的项目结构的图像(为清晰起见,添加了突出显示):
I tried to use such ways.我尝试使用这样的方式。 But it did not help但它没有帮助
1. "../../config/application.properties"
2. "src/resources/config/application.properties"
3. "../../resources/config/application.properties"
I do not pretend to be correct, but it worked:我不假装是正确的,但它有效:
resources
in src -> main
and moved the folder config
there.我在src -> main
创建了文件夹resources
并将文件夹config
移到那里。 I created the folder resources
in src -> test
and moved the all other folders there.我在src -> test
创建了文件夹resources
,并将所有其他文件夹移到那里。 The result is such a structure:结果是这样的结构:src
|----main
| |
| |----java
| |----resources
| | |----config
| | | |----application.properties
|
|----test
| |----resources
| | |----features
| | |----pipes
| | |----testdata
| | |----webdrivers
System.setProperty("TagConfigFile", "../../config/application.properties");
而是System.setProperty("TagConfigFile", "../../config/application.properties");
I wrote System.setProperty("TagConfigFile", "./config/application.properties");
我写了System.setProperty("TagConfigFile", "./config/application.properties");
in RunnerTest.java
class在RunnerTest.java
类中If someone offers a more beautiful working solution, I will be very glad如果有人提供更漂亮的工作解决方案,我会很高兴
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.