Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

properties files, initial directory, etc...

 
Ben Carlson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone!
I am having a fair bit of trouble with properties files, namely how to ensure that they are in the same location on various platforms without recompiling.
I develop in Windows at work, and on Linux at home, and every time I transfer code, I have to edit my classes and recompile so they have the proper path... example:
c:\apps\search\.properties //windows
/home/bcarlson/apps/search/.properties //Linux
I would have assumed(silly me!) that I should be able to place a .properties file in a dir in the classpath and just use:
Properties defaultProps = new Properties();
try {
defaultProps.load(new FileInputStream(".properties");
} catch (IOException e) {
....etc...
to grab from my /home/bcarlson/apps/search dir...? any thoughts? Examples of how you've solved this? I'm probably missing something simple, because I assume most people use .properties files on a regular basis... ?
Thanks so much!!
Ben Carlson
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could take in the location of the properties file as a parameter.
>java myAppp MyPropertiesFileLocation.properites
Then you invoke your application using a .bat file or whatever (this would need to be specific per platform).
 
Ben Carlson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a really good idea, one I hadn't thought of! DOH!
Ok, not to beat a dead horse, but, on the same train of thought, is there a way to reference a txt/xml/html file using the class style path (i.e. "com.foo.bar.xml")? I can see some issues, but it seems reasonable that others would have similar problems... ?
Thanks again for your quick answer, Cindy!
Ben Carlson
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so. The problem is that the claspath stuff is interpreted by the compiler - so Sun could re-work syntax all that they wanted.
However file locations are eventually passed to the OS to work with - and the OS expects their syntax to be honored.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Carlson:
[B]I would have assumed(silly me!) that I should be able to place a .properties file in a dir in the classpath and just use:to grab from my /home/bcarlson/apps/search dir...? any thoughts?[/B]
Easy. Try this.Put the ".properties" file in the same location as the .class file for the above class. If you want the properties to reside in the root of the class hierarchy, use getClass().getClassLoader().getResourceAsStream().
- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic