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

Discussion in 'Java' started by Wardie, Nov 7, 2006.

  1. Wardie

    Wardie Guest

    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
     
    Wardie, Nov 7, 2006
    #1
    1. Advertising

  2. Wardie

    Daniel Pitts Guest

    Wardie wrote:
    > 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.
     
    Daniel Pitts, Nov 7, 2006
    #2
    1. Advertising

  3. Wardie wrote:
    >
    > 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."
     
    RedGrittyBrick, Nov 7, 2006
    #3
  4. RedGrittyBrick wrote:
    > Wardie wrote:
    > >
    > > 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.
     
    Andrew Thompson, Nov 8, 2006
    #4
  5. Wardie

    Wardie Guest

    (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
     
    Wardie, Nov 8, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iain
    Replies:
    3
    Views:
    450
    Wouter van Vugt
    Nov 25, 2005
  2. Duncan Smith
    Replies:
    4
    Views:
    334
  3. Replies:
    2
    Views:
    355
    subeen
    Jul 6, 2008
  4. viza
    Replies:
    17
    Views:
    698
    santosh
    Jul 9, 2008
  5. InertEmployer
    Replies:
    9
    Views:
    1,423
    Jonathan N. Little
    Aug 31, 2011
Loading...

Share This Page