简体   繁体   中英

add properties file to jar

I have a properties file config.properties that I am accessing in a java project in Eclipse.

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

This works fine when I run the project in 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:

java.io.FileNotFoundException: config.properties

How can I package my property file so it is included and used in my jar?

When exporting your JAR, in the "Select the resources to export" panel, you need to put a checkmark next to "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 props = new Properties();
props.load(PropsSaver.class.getResourceAsStream("/config.properties"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM