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"));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top