Setting properties (not necessarily "Properties") in Java 5.0

W

Wardie

Hi all,


I have been searching around the archives but can't find an easily
digestible answer to my (simple) question.

Question : I'm using Java 5.0, what's the best way to set properties in
my code?

I currently use basic Java Properties, but I'm not sure that's the best
route these days. It certainly works - but I'd like to have macro
substitutions, along the lines of

myBase = C:/base
myApp = $myBase/bin/myAppName

which I know I can get from various thirdparty implementations.

I'm also having problems with where I locate the properties files. At
present it/they sit in the top level of my classes dir, accessed like
this ...

URL propertiesFileUrl = MyMainClass.class.getClassLoader().
getResource(filename);

.... but if I packed that up as a JAR it makes it hard for people to
change the property settings. What if the user wants to override
settings - does this have to be from the command line?

I've reasd about the Preference API but don't know if that's the
answer.

Sigh.

Any opinions on this would be apprecaited - I don't want to start a
religious war on the topic though :eek:)

Many thanks,
Chris
 
D

Daniel Pitts

Wardie said:
Hi all,


I have been searching around the archives but can't find an easily
digestible answer to my (simple) question.

Question : I'm using Java 5.0, what's the best way to set properties in
my code?

I currently use basic Java Properties, but I'm not sure that's the best
route these days. It certainly works - but I'd like to have macro
substitutions, along the lines of

myBase = C:/base
myApp = $myBase/bin/myAppName

which I know I can get from various thirdparty implementations.

I'm also having problems with where I locate the properties files. At
present it/they sit in the top level of my classes dir, accessed like
this ...

URL propertiesFileUrl = MyMainClass.class.getClassLoader().
getResource(filename);

... but if I packed that up as a JAR it makes it hard for people to
change the property settings. What if the user wants to override
settings - does this have to be from the command line?

I've reasd about the Preference API but don't know if that's the
answer.

Sigh.

Any opinions on this would be apprecaited - I don't want to start a
religious war on the topic though :eek:)

Many thanks,
Chris

I don't have much experience with the Preference API, but from what
I've seen, that is indeed a suitable answer for what you're looking for.
 
R

RedGrittyBrick

Wardie said:
I'm also having problems with where I locate the properties files. At
present it/they sit in the top level of my classes dir, accessed like
this ...

URL propertiesFileUrl = MyMainClass.class.getClassLoader().
getResource(filename);

... but if I packed that up as a JAR it makes it hard for people to
change the property settings. What if the user wants to override
settings - does this have to be from the command line?

I'd keep the default properties there but allow users to save settings
to a properties file located in their home directory.

"A property list can contain another property list as its "defaults";
this second property list is searched if the property key is not found
in the original property list."
 
A

Andrew Thompson

RedGrittyBrick said:
....
I'd keep the default properties there but allow users to save settings
to a properties file located in their home directory.

Good call. To help prevent the possibility of our program's
settings overriding the settings of another program, it is also
a good idea to pop settings into a *sub* directory of user.home,
based on the reverse package name of the main() class
(and no 'default package' app.s here, please!).

Andrew T.
 
W

Wardie

(Thanks for all the replies folks)

... To help prevent the possibility of our program's
settings overriding the settings of another program, it is also
a good idea to pop settings into a *sub* directory of user.home,
based on the reverse package name of the main() class
(and no 'default package' app.s here, please!).

I had thought about the default properties being in the .properties
file that "ships" with the classes/HAR file. Glad I wasn't going wrong
there.

The use of user.home as the location I find interesting. I've not
really made much use of prroperties up to this point. My Java work has
mainly been based around Tomcat/Servlets and a big slab of "business
objects" that all end up withing the WEB-INF/classes dir thereof. This
meant the I found there were already several places to stick my
settings. Now, however, I am doing a few Swing apps and a few
standalone/command line utils and this means I have to separate things
out from the "Big Ball of Mud" I had in the Tomcat world.

I was struggling to think of a common place for user properties to be
stored, I was thinking of a /properties or /config or even /xml dir at
the same level as the users /classes because I was assuming "users"
would be using the components as part of their own devleopment
projects. Obviously this is a blinkered view. Some will just want to
execute applications - and thus will not have a suitable dir structure.
So user.home seems a good place.

Hmmm - this is what comes of being a lone-Java jokey in a small
company. All the "users" I have at present are web-based or use a
WebStart invoked Swing app that currnetly has no property/configuration
support.

Thanks again,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top