Tyler Treat
Tyler Treat

Reputation: 14998

Referencing log4j config file within executable JAR

I am creating an executable JAR that uses a couple XML config files, one for the application and one for log4j. To reference my app config file, I do this:

InputStream config = Util.class.getResourceAsStream("/config/config.xml");

This works fine for my app config, but the problem is that I can't configure log4j like this. Here is the code that configures log4j:

DOMConfigurator.configure("/config/log4j.xml");

This won't work because the XML file is going to be stored within the packaged JAR. How can I configure log4j to use an XML or properties file within the JAR?

Upvotes: 3

Views: 4153

Answers (2)

adarshr
adarshr

Reputation: 62583

You can try

DOMConfigurator.configure(Util.class.getResource("/config/log4j.xml"));

Upvotes: 2

Ryan Gross
Ryan Gross

Reputation: 6515

You can use the URL version of the DOMConfigurator.configure method. The resource will have to be available at/config/log4j.xml.

DOMConfigurator.configure(Util.class.getResource("/config/log4j.xml")

Upvotes: 4

Related Questions