Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Wardie - 07 Nov 2006 14:33 GMT
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 :o)

Many thanks,
Chris
Daniel Pitts - 07 Nov 2006 19:47 GMT
> Hi all,
>
[quoted text clipped - 34 lines]
> 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.
RedGrittyBrick - 07 Nov 2006 22:46 GMT
> 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
[quoted text clipped - 6 lines]
> 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."
Andrew Thompson - 08 Nov 2006 02:04 GMT
> > I'm also having problems with where I locate the properties files.
...
> 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.
Wardie - 08 Nov 2006 09:46 GMT
(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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.