Explicitly Setting log4j Configuration File Location

I ran into an issue recently, where an existing log4j.xml configuration file was built into a jar file I was referencing and I was unable to get Java to recognize another file that I wanted it to use instead.  Fortunately, the solution to this problem is fairly straightforward and simple.

I was running a standalone application in linux, via a bash shell script; but this technique can be used in other ways too.  You simply add a parameter to the JVM call like the example below.

So the syntax is basically:

java -Dlog4j.configuration="file:" -cp   

Lets say I have a file named log4j.xml in /opt/tools/myapp/ which I want to use when my application runs, instead of any existing log4j.xml files.  This can be done by passing a JVM flag -Dlog4j.configuration to Java.

Here is an example:

java -Dlog4j.configuration="file:/opt/tools/myapp/log4j.xml" -cp $CLASSPATH  my.standalone.mainClass;

With that change, as long as your log4j file is set up properly, your problems should be behind you.

[amazon_link asins=’0071808558,B01LXGO1I2,1590594991,1617290068′ template=’ProductCarousel’ store=’openmindspace-20′ marketplace=’US’ link_id=’2bff41de-d2ae-11e6-b98d-b35baa63e07c’]

Leave a comment

Your email address will not be published. Required fields are marked *