log4j: locating log4j.proprties in same folder as jar

A

Alex Hunsley

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
 
A

Alex Hunsley

Alex said:
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
 
?

=?ISO-8859-15?Q?Fr=E9d=E9ric_G=E9din?=

Alex said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top