0

I have written an runnable java application(command line application). I put the log4j.xml in src/main/resources,and when I debug the application in eclipse, it works well.

But when I export it to an runnable jar file(exclude all required libraries and resources), and run it in command line. java -jar myapp.jar, the application cann't find the log4j.xml. the warning is:

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 in
fo.

And when I put the log4j.xml to the folder where to put the myapp.jar, it works again!

How can I use the log4j.xml packaged in the myapp.jar?

Dozer
  • 5,025
  • 11
  • 36
  • 52
  • You say "exclude all required libraries and resources". Does that mean that the `log4j.xml` file is not in the jar file you created? – NilsH Apr 19 '13 at 05:15
  • 1
    Please see [Java: problem running a jar file in command line](http://stackoverflow.com/questions/945962/java-problem-running-a-jar-file-in-command-line). By overview please use `java -cp my.jar my.package.MainClass` instead. – Charlee Chitsuk Apr 19 '13 at 05:17
  • I unzip the jar file,and I can find the `resources/log4j.xml` file – Dozer Apr 19 '13 at 05:22
  • http://stackoverflow.com/questions/7215462/java-log4j-problem-with-jar-executable – hurricane Aug 21 '14 at 13:40

2 Answers2

1

The problem is that your log4j.xml file ends up in the resources folder inside your jar. This means that either your source code structure is wrong, or Eclipse doesn't care about it. Your folder structure resembles a maven project. If it is, try using Maven to create the har file instead. That would put the log4j config file in the classpath root. Otherwise, you can try starting the jar file by adding the system property -Dlog4j.configuration=resources/log4j.xml

NilsH
  • 13,705
  • 4
  • 41
  • 59
0

I find my problem!~ I used maven,but extend the project by eclipse. When I use mvn assembly:assembly,it works well

Dozer
  • 5,025
  • 11
  • 36
  • 52