Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use Properties file contents

 
Hari Dhanakoti
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I had been using the properties file in JSP, JSF it works fine.

I want to implement the same here in the java code. Here i am using a web application where the property file is placed inside a package. I have some details like File Name, File Path, etc..

So, if i try to refer the file with "abc.properties". I get the exception as FileNotFoundException.

How can we refer them inside the project?
 
Joe Ess
Bartender
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you trying to load the properties file?
Where is the properties file stored?
I usually put properties files at the root of the classpath (the anonymous package), get a InputStream with Class.getResourceAsStream() and pass that to the Properties class.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is all about the JRE is unable to find/locate your properties file class.

It is because your CLASSPATH environment variable does NOT have the location to identify it and pick it up!

In case of a web application, it is better to keep it inside the "WEB-INF/classes" directory as this location is by default added into the classpath searched by the web container.

In case of plain Java application, you either add the location of this file to CLASSPATH environment or you place the files in any of the directories present in the CLASSPATH.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the way you pick up the file was mentioned by Joe Ess
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic