aspose file tools*
The moose likes Beginning Java and the fly likes Reading from a properties file for path declared in classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading from a properties file for path declared in classpath" Watch "Reading from a properties file for path declared in classpath" New topic
Author

Reading from a properties file for path declared in classpath

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

or use java.util.ResourceBundle
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading from a properties file for path declared in classpath