如jar所示,执行“系统找不到指定的属性文件". [英] Executing as jar says "System cannot find the properties file specified"

查看:81
本文介绍了如jar所示,执行“系统找不到指定的属性文件".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 src/main/resources 文件夹中有一个带有 app.properties 文件的gradle项目.

I have a gradle project with app.properties file present in the src/main/resources folder.

当我尝试使用jar执行项目时,它说无法读取指定的app.properties文件.

When i am trying to execute the project using jar, it says not able to read the app.properties file specified.

下面是读取属性文件的代码:

Below is the code to read the properties file :

  Properties properties = new Properties();  
  File file = new File("src/main/resources/app.properties");
  FileInputStream inputStream = new FileInputStream(file);
  properties.load(inputStream);

我正在使用以下命令来运行jar:

I am using below command to run the jar :

Java -jar App.jar

在此问题上的任何帮助将不胜感激.

Any help on this issue will be appreciated.

推荐答案

尝试使用getSystemResourceAsStream

 try (InputStream inputStream = ClassLoader.getSystemResourceAsStream("app.properties"))

这篇关于如jar所示,执行“系统找不到指定的属性文件".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆