我有一个使用 Cucumber 和 Maven 的项目,来自 Intellij IDEA。
我需要使用相对文件路径。现在我在 mac os 上工作,但随后该项目也会在 windows 上使用。
- 如何确定 macOS 中从 RunnerTest.java 到 application.properties 文件的相对路径?
- 为 Windows 定义相同的路径会有什么不同吗?
这是我的 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);
}
}
}
这是我的项目结构的图像(为了清晰起见,添加了突出显示):
我尝试过使用这样的方式。但这并没有帮助
1. "../../config/application.properties"
2. "src/resources/config/application.properties"
3. "../../resources/config/application.properties"
最佳答案
我并不假装是正确的,但它确实有效:
- 我在
src -> main
中创建了文件夹resources
并将文件夹config
移至此处。我在src -> test
中创建了文件夹resources
并将所有其他文件夹移至此处。结果是这样的结构:
src
|----main
| |
| |----java
| |----resources
| | |----config
| | | |----application.properties
|
|----test
| |----resources
| | |----features
| | |----pipes
| | |----testdata
| | |----webdrivers
- 而是
System.setProperty("TagConfigFile", "../../config/application.properties");
我在RunnerTest.java
类中编写了System.setProperty("TagConfigFile", "./config/application.properties");
如果有人提供更漂亮的工作解决方案,我将非常高兴
关于java - 如何定义文件的相对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60188436/