This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 19651
    
  18

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: 19651
    
  18

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.
 
wood burning stoves
 
subject: Reading from a properties file for path declared in classpath
 
Similar Threads
Adding class-path to manifest
How to read properties files from outside Container?
Where to keep the properties file
Properties file path hard coded
New folder inside an existing plugin