[英]add properties file to jar
I have a properties file config.properties that I am accessing in a java project in Eclipse. 我有一个属性文件config.properties,可以在Eclipse的Java项目中访问该文件。
BufferedReader bfr = new BufferedReader(new FileReader(new File("config.properties")));
This works fine when I run the project in Eclipse. 当我在Eclipse中运行项目时,此方法工作正常。 But when I export the project as a jar file, config.properties is not included in the jar and when I run the jar I get the following error:
但是,当我将项目导出为jar文件时,jar中不包含config.properties,运行jar时出现以下错误:
java.io.FileNotFoundException: config.properties java.io.FileNotFoundException:config.properties
How can I package my property file so it is included and used in my jar? 如何打包我的属性文件,使其包含在jar中并在其中使用?
When exporting your JAR, in the "Select the resources to export" panel, you need to put a checkmark next to "config.properties". 导出JAR时,在“选择要导出的资源”面板中,需要在“ config.properties”旁边打勾。
Then, you'll need to change the way you load the properties file: 然后,您需要更改加载属性文件的方式:
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/config.properties")));
Also properties files usually aren't read that way -- try Properties.load: 通常,属性文件通常也不用这种方式读取-尝试使用Properties.load:
Properties props = new Properties();
props.load(PropsSaver.class.getResourceAsStream("/config.properties"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.