将属性文件添加到jar

时间:2013-10-29 16:23:02

标签: java jar properties-file

我有一个属性文件config.properties,我正在Eclipse中的java项目中访问。

 BufferedReader  bfr = new BufferedReader(new FileReader(new File("config.properties")));

当我在Eclipse中运行项目时,这很好用。但是当我将项目导出为jar文件时,jar中不包含config.properties,当我运行jar时,我收到以下错误:

java.io.FileNotFoundException:config.properties

如何打包我的属性文件,以便它包含在我的jar中使用?

1 个答案:

答案 0 :(得分:0)

导出JAR时,在“选择要导出的资源”面板中,需要勾选“config.properties”旁边的复选标记。

然后,您需要更改加载属性文件的方式:

BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/config.properties")));

通常也不会以这种方式读取属性文件 - 尝试使用Properties.load:

Properties props = new Properties();
props.load(PropsSaver.class.getResourceAsStream("/config.properties"));
相关问题