• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading from a properties file for path declared in classpath

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a properties file whose path I have declared in class path environment variable. I want to now create a standalone java program to read the contents of this file. Please help me how to do it.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the file is in a folder that's part of the class path, an input stream to the file should available through Class.getResourceAsStream or ClassLoader.getResourceAsStream. You can pass this input stream to the load method of java.util.Properties.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or use java.util.ResourceBundle
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get exceptions using any solutions.

At last, the only solution that worked was to get the properties file in the same folder that i have my code and access it this way


InputStream is = Encryption.class.getResourceAsStream("Prop.properties");
Properties prop = new Properties();
prop.load(is);


But, i really want to access it from classpath.

Please suggest
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're probably simply using the wrong resource name. With Class.getResourceAsStream the resource name would need to be absolute, i.e. start with a /. "/Prop.properties" probably would work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic