0
votes

I come across log4j initialization error, while deploying multiple war files in tomcat. I have all of the dependencies in Tomcat/lib, except log4j.jar, which is in webApp's web-inf , lib folder. and log4j.properties in web-inf/classes folder.

Since App1.war and App2.war have their own log4j, i have kept the log4j.jar in respective web Apps lib folder.

Then I get below error,

29-May-2017 13:15:01.871 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Initializing log4j from [file:D:\Users\vissurve\Softwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\sho-sp-item\WEB-INF\classes\log4j.properties]
29-May-2017 13:15:01.875 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
 java.lang.NoClassDefFoundError: org/apache/log4j/PropertyConfigurator
    at org.springframework.util.Log4jConfigurer.initLogging(Log4jConfigurer.java:78)
    at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:154)
    at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:49)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4717)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5179)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:952)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1823)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.PropertyConfigurator
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 16 more

29-May-2017 13:15:01.887 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Shutting down log4j
29-May-2017 13:15:01.888 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStop Exception sending context destroyed event to listener instance of class org.springframework.web.util.Log4jConfigListener
 java.lang.NoClassDefFoundError: org/apache/log4j/LogManager
    at org.springframework.util.Log4jConfigurer.shutdownLogging(Log4jConfigurer.java:123)
    at org.springframework.web.util.Log4jWebConfigurer.shutdownLogging(Log4jWebConfigurer.java:172)
    at org.springframework.web.util.Log4jConfigListener.contextDestroyed(Log4jConfigListener.java:54)
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4764)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5401)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:187)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:952)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1823)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.LogManager
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 17 more

Please suggest. I have googled a lot, there are similar post but some how my issue does not get solved with those solutions.

Each war's web.xml is like below,

<context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>SomeUniqueValue</param-value>
</context-param>

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>file:${catalina.base}\webapps\MyAppName\WEB-INF\classes\log4j.properties</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

It works fine if I move log4j.jar in tomcat/lib folder instead but then it starts logging all logs in one log file.

Please suggest, why can't it find the dependencies if they are in tomcat/lib.

1

1 Answers

0
votes

This is how I fixed the above problem.

Tomcat : PreResources tag not loading jars

Also, my log4j and apachecommons and all other jars are now externalized and does not make my war file huge and bulky. A common tomcat's context.xml loadeds all jars.