views:

36

answers:

3

I'm building a project with ant and during the build I'd like to bundle a log4j.xml file directly into the .jar to make distribution simpler. I've been successful in adding the file to the .jar, but it doesn't seem to be recognised.

What do I need to do to ensure the file is recognised and used by log4j? I'm new to Java, so a clear description, with examples if possible, would be great!

A: 

This would somehow works.

static // loads before anything else can
{
   /*
    * If log4j.configuration system property isn't set,
    * then assume I'm inside a jar and configure
    * log4j using the config file that shipped in the jar.
    */
   if (System.getProperty("log4j.configuration") == null)
   {
      URL url = ClassLoader.getSystemResource("log4j.xml");
      DOMConfigurator.configure(url);
   }
}
SiLent SoNG
somehow? You mean?
Thorbjørn Ravn Andersen
A: 

add file in the project's resource

org.life.java
A: 

How is your application being invoked?

As long as the JAR is on the classpath, any file contained in it is also in the classpath. In other words, log4j should have no problem in finding some.jar!log4j.xml, if some.jar is on the classpath to begin with.

Are there other log4j configuration files being picked up from other locations on the classpath? You can add -Dlog4j.debug to the startup of your application to have log4j print out debugging information about which configuration file is being used. It is very possible that another configuration file bundled in another JAR is being found first.

matt b
`java -jar myapp.jar`; I'll be providing a single jar file to our users, so everything is included within that jar file.
digitala
I believe you need to explicitly include the log4j.xml on the `Classpath:` attribute in the `MANIFEST.MF` file. Launching processes with `-jar` is a bit different than "normal"
matt b