log4j: locating log4j.proprties in same folder as jar

Discussion in 'Java' started by Alex Hunsley, May 28, 2004.

  1. Alex Hunsley

    Alex Hunsley Guest

    I'm using log4j (via Jakarta common-logging but that part is irrelevant).
    Things work fine with the configuration file, log4j.properties, in the jar file
    I'm making that contains the code. However, I'd like the log4j.properties to be
    in the same folder as the jar file, not inside the jar, as this makes it much
    more accessible (read: editable). Now since log4j looks at a system setting for
    the location of the jar file I can do this:

    System.setProperty("log4j.configuration",
    jarFileLocation); // jarFileLocation is a String

    I find out the value of jarFileLocation by doing this in the code:

    java.net.URL classURL =
    new TestApp().getClass().getResource(className);

    .... and then by doing hacky ugly things to the resulting URL to get a string.
    This works, but is not very satisfactory to me; seems too hacky and prone to
    break somehow.

    So: is there any better way to get log4j to read its config file from the same
    dir as the jar containing the currently running code? Or is this hack the best
    there is?

    thanks
    alex
     
    Alex Hunsley, May 28, 2004
    #1
    1. Advertising

  2. Alex Hunsley

    Alex Hunsley Guest

    Alex Hunsley wrote:

    > I'm using log4j (via Jakarta common-logging but that part is
    > irrelevant). Things work fine with the configuration file,
    > log4j.properties, in the jar file I'm making that contains the code.
    > However, I'd like the log4j.properties to be in the same folder as the
    > jar file, not inside the jar, as this makes it much more accessible
    > (read: editable). Now since log4j looks at a system setting for the
    > location of the jar file I can do this:
    >
    > System.setProperty("log4j.configuration",
    > jarFileLocation); // jarFileLocation is a String
    >
    > I find out the value of jarFileLocation by doing this in the code:
    >
    > java.net.URL classURL =
    > new TestApp().getClass().getResource(className);
    >
    > ... and then by doing hacky ugly things to the resulting URL to get a
    > string.
    > This works, but is not very satisfactory to me; seems too hacky and
    > prone to break somehow.
    >
    > So: is there any better way to get log4j to read its config file from
    > the same dir as the jar containing the currently running code? Or is
    > this hack the best there is?
    >
    > thanks
    > alex


    Okay, now to answer myself again :/

    Exceedingly simple way to get this work, and no hacky hacky:

    Ensure that the classpath that is set in your jar's manifest includes "." (i.e.
    current directory). Then, log4j will pick up the log4j.properties in the same
    folder as the jar you're running.

    So simple and why didn't I think of it first?

    alex
     
    Alex Hunsley, May 28, 2004
    #2
    1. Advertising

  3. Alex Hunsley wrote:

    > Alex Hunsley wrote:
    >
    >> I'm using log4j (via Jakarta common-logging but that part is
    >> irrelevant). Things work fine with the configuration file,
    >> log4j.properties, in the jar file I'm making that contains the code.
    >> However, I'd like the log4j.properties to be in the same folder as the
    >> jar file, not inside the jar, as this makes it much more accessible
    >> (read: editable). Now since log4j looks at a system setting for the
    >> location of the jar file I can do this:
    >>
    >> System.setProperty("log4j.configuration",
    >> jarFileLocation); // jarFileLocation is a String
    >>
    >> I find out the value of jarFileLocation by doing this in the code:
    >>
    >> java.net.URL classURL =
    >> new TestApp().getClass().getResource(className);
    >>
    >> ... and then by doing hacky ugly things to the resulting URL to get a
    >> string.
    >> This works, but is not very satisfactory to me; seems too hacky and
    >> prone to break somehow.
    >>
    >> So: is there any better way to get log4j to read its config file from
    >> the same dir as the jar containing the currently running code? Or is
    >> this hack the best there is?
    >>
    >> thanks
    >> alex

    >
    > Okay, now to answer myself again :/
    >
    > Exceedingly simple way to get this work, and no hacky hacky:
    >
    > Ensure that the classpath that is set in your jar's manifest includes "."
    > (i.e. current directory). Then, log4j will pick up the log4j.properties in
    > the same folder as the jar you're running.


    Another solution is to create in the user home directory a directory
    specific to your applications which contains the log4j.properties as well
    as any other configuration file you may need. And, of course you can add
    this place to your classpath. The user home directory can be known through
    System.getProperty("user.home").
    This maybe less simple than your classpath solution but probably more
    convenient as it will allow you to run the application from any directory.
    Your solution would force you to create a log4j.properties file in any
    directory from which you want to launch your application.

    Frederic
     
    =?ISO-8859-15?Q?Fr=E9d=E9ric_G=E9din?=, May 28, 2004
    #3
    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. Arnold Peters
    Replies:
    0
    Views:
    590
    Arnold Peters
    Jan 5, 2005
  2. muttley
    Replies:
    0
    Views:
    2,736
    muttley
    Oct 20, 2005
  3. cyberco
    Replies:
    4
    Views:
    3,783
    Roedy Green
    Feb 14, 2006
  4. Jezzer

    OJB.proprties & websphere

    Jezzer, Mar 14, 2006, in forum: Java
    Replies:
    1
    Views:
    356
    Jezzer
    Mar 16, 2006
  5. faceless

    locating resources in a jar

    faceless, May 1, 2008, in forum: Java
    Replies:
    0
    Views:
    282
    faceless
    May 1, 2008
Loading...

Share This Page