PaulK
PaulK

Reputation: 653

Loading log4j2.xml file from same directory as jar file

I have a directory /release/ containing my application.jar as well as the log4j2.xml file. When I run the application with java -jar application.jar it will not pick up the log4j2.xml configuration file. It works if I explicitly specify the location of the xml file like so

java -Dlog4j.configurationFile=log4j2.xml -jar application.jar

Is there a way I can instruct my program to always pick up the configuration file from the same directory as the jar resides in? I want to keep the program as simple as possible and not require the user to set an awkward system property when running the application.

Upvotes: 0

Views: 2317

Answers (1)

Remko Popma
Remko Popma

Reputation: 36754

Without the system property, log4j-2.0 will look for a log4j2.xml file in the classpath. So I'm reading your question as: how can I start my app with java -jar application.jar and have the log4j2.xml file in the classpath?

This depends on whether your application.jar file has a Class-Path entry in its manifest (http://docs.oracle.com/javase/8/docs/technotes/tools/windows/findingclasses.html#sthref8 ).

You could simply include the log4j2.xml file in the application.jar file, of course. If you want to avoid that, you could add the directory containing the log4j2.xml file to the Class-Path entry in the JAR file's manifest. (This could be the "." current directory.)

Upvotes: 1

Related Questions