Unable to initialize log4j
I'm trying to initialize log4j-1.2.8 for my application by creating a listener class implemented from the ApplicationLifecycleListener of Weblogic 9.2. When I deploy the application, I'm getting following exceptions:
java.lang.NoClassDefFoundError: org/apache/log4j/spi/RepositorySelector
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(Secur开发者_JS百科eClassLoader.java:124)
at weblogic.utils.classloaders.GenericClassLoader.defineClass(GenericClassLoader.java:338)
at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:291)
at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:259)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at weblogic.utils.classloaders.GenericClassLoader.loadClass(GenericClassLoader.java:158)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at myApp.LoggerStartupService.preStart(LoggerStartupService.java:40)
at weblogic.application.internal.flow.BaseLifecycleFlow$PreStartAction.run(BaseLifecycleFlow.java:187)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.application.internal.flow.BaseLifecycleFlow$BaseAction.invoke(BaseLifecycleFlow.java:95)
at weblogic.application.internal.flow.BaseLifecycleFlow.preStart(BaseLifecycleFlow.java:53)
at weblogic.application.internal.flow.HeadLifecycleFlow.prepare(HeadLifecycleFlow.java:199)
at weblogic.application.internal.BaseDeployment$1.next(BaseDeployment.java:615)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:26)
at weblogic.application.internal.BaseDeployment.prepare(BaseDeployment.java:191)
at weblogic.application.internal.DeploymentStateChecker.prepare(DeploymentStateChecker.java:147)
at weblogic.deploy.internal.targetserver.AppContainerInvoker.prepare(AppContainerInvoker.java:61)
at weblogic.deploy.internal.targetserver.operations.ActivateOperation.createAndPrepareContainer(ActivateOperation.java:189)
at weblogic.deploy.internal.targetserver.operations.ActivateOperation.doPrepare(ActivateOperation.java:87)
at weblogic.deploy.internal.targetserver.operations.AbstractOperation.prepare(AbstractOperation.java:217)
at weblogic.deploy.internal.targetserver.DeploymentManager.handleDeploymentPrepare(DeploymentManager.java:718)
at weblogic.deploy.internal.targetserver.DeploymentManager.prepareDeploymentList(DeploymentManager.java:1185)
at weblogic.deploy.internal.targetserver.DeploymentManager.handlePrepare(DeploymentManager.java:247)
at weblogic.deploy.internal.targetserver.DeploymentServiceDispatcher.prepare(DeploymentServiceDispatcher.java:157)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.doPrepareCallback(DeploymentReceiverCallbackDeliverer.java:157)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.access$000(DeploymentReceiverCallbackDeliverer.java:12)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer$1.run(DeploymentReceiverCallbackDeliverer.java:45)
at weblogic.work.ServerWorkManagerImpl$WorkAdapterImpl.run(ServerWorkManagerImpl.java:518)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)
where myApp.LoggerStartupService
is the class implemented from ApplicationLifecycleListener
.
I checked the classpath in my scripts and its set properly for all dependencies required for log4j. Appenders & Categories are also there in the log4j.xml. Looks like I'm missing something. Any ideas on what the problem may be?
java.lang.NoClassDefFoundError: org/apache/log4j/spi/RepositorySelector
This means that the in the message mentioned class is missing in the classpath during runtime (while it was available during compiletime of the calling class in question, that's the difference with ClassNotFoundException
).
As this is used by Log4j itself, it's thus missing in the Log4j JAR file. The RepositorySelector
javadoc learns us that it's introduced in Log4j 1.2. This would mean that there's a collision with another and older-versioned Log4j JAR file in the classpath which got precedence in classloading. It's likely somewhere hidden in one of the default classpaths of Weblogic. I don't do Weblogic, but as hinted by others, you can also try to change the classloading order, if it's supported by Weblogic. Consult its documentation.
where myApp.LoggerStartupService is the class implemented from ApplicationLifecycleListener.
Ok, so you implemented a startup class.
I checked the classpath in my scripts and its set properly for all dependencies required for log4j. Appenders & Categories are also there in the log4j.xml.
How did you setup that classpath? How did you package your startup class and where did you put it? Also, where is log4j.jar
, which one do you use? Your startup class (and its dependencies) must be added to the server CLASSPATH
. Is this what you did? Refer to Add startup and shutdown classes to the classpath in the official documentation for the details on how to do this.
I'd like to get confirmation of these points (but the final answer is that you should use a MANIFEST.MF in the jar of your startup class to reference third party libraries).
For my case, I using log4j 2.x and cannot find org/apache/log4j/spi/RepositorySelector, because there is no such API in version 2.x, to use it, need add the dependency of log4j-1.2-api
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.5</version>
</dependency>
after
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
精彩评论