Reputation: 515
I am creating a java application in Eclipse Helios.I have created a class in which i need to have data from the .properties file which i added in the folder containing the solution .Through this code i can accesss the value from the .properties file when i try to run it in Eclipse.
The code is:
Properties prop = new Properties();
prop.load(new FileInputStream("config.properties"));
_url = prop.getProperty("url");
Through this code i am getting the correct value of the Url .
After this i have created an executable jar of my project .When i tried to execute the jar using command line then i got a FileNotFoundException ,means the jar is not able to locate the .properties file.
I have kept the .properties file in the same folder as of Jar file as we have the provision of editing the .properties file.
Since i started working in Java for only 4 days ,i am unable to figure out about where to place the .properties file and how to access it.
Please help.
Upvotes: 0
Views: 742
Reputation: 9579
For jar try to use:
prop.load(getClass().getClassLoader().getResourceAsStream("config.properties"));
if you call from static context use:
prop.load(YourClassName.class.getClassLoader().getResourceAsStream("config.properties"));
Upvotes: 1
Reputation: 44881
You need to ask the class loader to get a resource. See http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html
Properties prop = new Properties();
prop.load(classLoader.getResourceAsStream("config.properties"));
_url = prop.getProperty("url");
Upvotes: 0