Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从IDE和JAR外部加载.properties文件_Java_Maven_Properties_Jar - Fatal编程技术网

Java 从IDE和JAR外部加载.properties文件

Java 从IDE和JAR外部加载.properties文件,java,maven,properties,jar,Java,Maven,Properties,Jar,我在maven项目的resources文件夹下有一个app.properties文件,如下所示(简化): 这段代码在我从IDE内部运行时正确加载属性,但异常失败 Exception in thread "main" java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properti

我在maven项目的
resources
文件夹下有一个
app.properties
文件,如下所示(简化):

这段代码在我从IDE内部运行时正确加载属性,但异常失败

Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at cz.muni.fi.fits.ApplicationInitializer.main(ApplicationInitializer.java:18)
当尝试以JAR文件的形式运行时

为了创建jar文件,我在
pom.xml
文件中结合使用
maven shade plugin
maven jar plugin
(用于将属性文件排除在jar之外)和
maven resources plugin
(用于将属性文件复制到特定文件夹),如下所示:


org.apache.maven.plugins
maven阴影插件
2.3
包裹
阴凉处
cz.muni.fi.fits.ApplicationInitializer
org.apache.maven.plugins
maven jar插件
2.5
**/*.物业
maven资源插件
2.7
复制资源
包裹
复制资源
${basedir}/目标
src/main/resources
然后,我将main方法中的代码切换到以下方法:

Properties props = new Properties();

String path = "./app.properties";

try (FileInputStream file = new FileInputStream(path)) {
    props.load(file);
} catch (IOException e) {
    e.printStackTrace();
}

System.out.println(props.getProperty("property"));
在运行JAR时,我设法从文件中加载属性,但这次在IDE中运行时,我无法加载属性,它以与上面相同的异常结束

所以我的问题是:如何设置文件路径(或
pom.xml
file?),以便我能够加载从IDE和JAR文件运行的属性


提前感谢:)

目前的工作方式取决于java进程的工作目录。通常,在启动应用程序时,您的命令行(也称为shell)指向的就是这个位置

在大多数IDE上,您可以在启动器设置中配置此目录。(对于eclipse,它位于第二个选项卡“Arguments”(参数)上),因此您需要访问目标目录(对于eclipse,有一个按钮“Workspace…”)

特别是在intelliJ中,您可以在运行/编辑配置下找到此设置。。。这将打开如下窗口: 您可以在那里编辑工作目录。您只需在末尾添加target

编辑:
实际上,您已经在末尾添加了src/main/resources。

在Java代码中,像这样读取
app.properties
文件:

final String ROOT_PATH = "custom.path"; // Or whatever you most like
final String PROPERTIES_FILE = "app.properties";

// start :: try-catch here to manage I/O resources
File directory = new File(System.getProperty(ROOT_PATH), "conf");
File file = new File(directory, PROPERTIES_FILE);
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
// ...
// end :: try-catch here to manage I/O resources
然后您必须找到设置/声明自定义路径的方法:

  • 将其声明为环境变量
  • 使用
    -Dcustom.path=/somewhere/in/your/filesystem
    在运行时设置它(环境属性)(我更喜欢这个,除非路径是固定的和/或在不同的应用程序之间共享)
然后,最终您需要将
app.properties
文件复制/放入
/where/in/your/filesystem/conf
中。为什么在
/conf
中?因为您在声明
目录
字段时使用它。如果你不想把它放在那里,就不要设置它并删除
,“conf”
部分

此外,要“本地”(在IDE中)运行它,请使用VM选项设置(IntelliJ IDEA):


我在
target
中看到,您拥有
myApp.jar
app.properties
。你能检查一下
app.properties
是否在
.jar
文件中吗?不,不是,这就是为什么我使用
maven-jar-plugin
将其从jar文件中排除的原因。你为什么试图将属性文件放在jar文件之外?如果你这样做,你就不能使用
getResourcesAsStream()
。我想把它放在外面,因为我希望用户只需编辑属性文件(没有输入参数)就可以编辑应用程序的配置。那么你的建议是什么?很抱歉,我不明白你的答案。我的建议是配置你的IDE,用正确的工作目录启动java进程。您使用的IDE是什么?我使用的是IntelliJ IDEA 14这并不能解决加载文件的问题,因为只有在使用maven打包应用程序时,属性才会复制到
目标
目录。因此,我不需要
resources
文件夹,我会将属性文件视为普通文件。但如果问题是:我如何才能找到一个文件,不管它放在哪里,答案是‘你不能’。如果问题是:我有两个用例。“我该如何处理?”然后我给出了答案。谢谢,这看起来很有希望,但将路径定义为环境变量对我来说有点过头了,而且我不能使用运行时参数,因为它会弄乱我当前的参数。@Supermartzin我能理解“环境变量”部分,但运行时参数(环境属性)是不正确的你想要多少就有多少,他们不会互相干扰当然,你是对的,但这会降低我程序的清晰度,我想避免这种情况。无论如何,谢谢你的建议。
Properties props = new Properties();

String path = "./app.properties";

try (FileInputStream file = new FileInputStream(path)) {
    props.load(file);
} catch (IOException e) {
    e.printStackTrace();
}

System.out.println(props.getProperty("property"));
final String ROOT_PATH = "custom.path"; // Or whatever you most like
final String PROPERTIES_FILE = "app.properties";

// start :: try-catch here to manage I/O resources
File directory = new File(System.getProperty(ROOT_PATH), "conf");
File file = new File(directory, PROPERTIES_FILE);
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
// ...
// end :: try-catch here to manage I/O resources