简体   繁体   中英

log4j warning (Please initialize the log4j system properly.) when running java application as jar

I am now creating a standalone java application which needs to be run as an executable jar.I have configured log4j for proper logging. It writes all the log files when running the application from eclipse.But when I ran the executable jar it comes up with a warning as ** log4j:WARN No appenders could be found for logger log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. ** Can someone please suggest how to solve the issue?

You may not have a valid or correct configuration. Add this to your main method.

BasicConfigurator.configure();

Failing that , the properties file may not be set up properly if you are using maven in the pom.

The other case is that you have two different logging frameworks in use. Use one or the other.

Another try would be doing something like this to set the property or log4j rootlogger :

 Properties prop = new Properties(); 
 prop.setProperty("log4j.rootLogger", "WARN");
 PropertyConfigurator.configure(prop);

Also you can try adding a system property to the command line when you start Java:

-Dlog4j.configuration=file:///path/to/your/log4j.properties

Last ditch effort is:

Move Your log4j.properties file into the src folder. Open Windows explorer, browse to the directory where your project resides in. Browse to bin directory and delete all the folders with in it.

Now in eclipse click project-->clean-->OK

This would force it to build the project again,all the contents delete from bin directory will be recreated. Then use the executable jar again when you newly build it.

I tried adding the below configuration on my main class. PropertyConfigurator.configure("/opt/resources/log4j.properties"); and it did work. Eventhough it is still showing the warning message but it is writing the log files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM