Java中的文件路径(Linux) [英] File paths in Java (Linux)
问题描述
我已经创建了一个Java应用程序,用于从 conf.properties
文件中加载一些配置,该文件位于 src /
文件夹。
I have created a Java application that loads some configurations from a file conf.properties
which is placed in src/
folder.
当我在Windows上运行这个应用程序,它完美的工作。但是,当我尝试在Linux上运行它时,会引发这个错误:
When I run this application on Windows, it works perfectly. However when I try to run it on Linux, it throws this error:
java.io.FileNotFoundException: src/conf.properties (No such file or directory)
推荐答案
将你的应用程序打包到一个jar文件中,该文件又包含属性文件,你应该使用下面的方法。这是分发Java程序的标准方式。
If you've packaged your application to a jar file, which in turn contains the properties file, you should use the method below. This is the standard way when distributing Java-programs.
URL pUrl = this.getClass().getResource("/path/in/jar/to/file.properties");
Properties p = new Properties();
p.load(pUrl.openStream());
路径中的/指向jar文件中的根目录。
The / in the path points to the root directory in the jar file.
这篇关于Java中的文件路径(Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!