This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

problem with path for properties file  RSS feed

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
not sure if this question goes here or in java for beginners but here goes
I've got a couple of pieces of info that will change when I move my servlet from a development environment to production. Things like the URL and database connection info etc. I'd like to store this stuff in a properties file rather than hardcode them into the servlet. If I read the properties file like this
properties.load(new FileInputStream("properties.file"));
it has to be in my home directory (I'm running linux)
I want to use a relative path so it works when I move around to different enviroments. Where should the file be if I specify it like this
properties.load(new FileInputStream("./properties.file"));
???
Thanx
Dave
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always use classpath-based URLs, not directory-based ones.
URL myPropsURL = getClass().getResource("/myprops.properties");
The above finds myprops.properties rooted in the classpath.
Use the URL instead of a path name in APIs that allow either.
Or, to open the stream directly:
InputStream myPropsStream = getClass().getResourceAsStream("/myprops.properties");
 
Dave Robbins
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yikkes!!
got an answer in 8 minutes
Thanx Billy
Dave
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!