0

I have a java web project which includes lo4j2. It is deploy on tomcat instance locally on my PC. I was debugging the java project. When debugger is on the instruction of Certificate class contructor and I see when the instruction 'CryptoProviderTools.installBCProvider();' is executed, it throws the following exception:

Servlet.service() for servlet [appServlet] in context with path [/myWebApp] threw exception [Request processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/Logger] with root cause
java.lang.ClassNotFoundException: org.apache.log4j.Logger
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1109)
    at org.cesecore.util.CryptoProviderTools.<clinit>(CryptoProviderTools.java:39)
    at common.Certificate.<init>(Ejbca.java:79)

The message shows 'org/apache/log4j/Logger' as if Java project is using log4j version 1 but it's actually using the log4j version 2. I don't know what does log4j1 have to do.

I got other java web project where CryptoProviderTools.installBCProvider() is working with lo4j2.

I'm showing you pom.file, log4j2.properties and Certificate.java files.

pom.xml file

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.17.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.17.1</version>
</dependency>
<dependency>
    <groupId>cert-cvc-6</groupId>
    <artifactId>cert-cvc-6</artifactId>
    <version>6.0</version>
</dependency>
<dependency>
    <groupId>ejbca-util-6</groupId>
    <artifactId>ejbca-util-6</artifactId>
    <version>6.0</version>
</dependency>
<dependency>
    <groupId>ejbca-ws-cli-6</groupId>
    <artifactId>ejbca-ws-cli-6</artifactId>
    <version>6.0</version>
</dependency>
<dependency>
    <groupId>ejbca-ws-6</groupId>
    <artifactId>ejbca-ws-6</artifactId>
    <version>6.0</version>
</dependency>
<dependency>
    <groupId>cesecore-common-6</groupId>
    <artifactId>cesecore-common-6</artifactId>
    <version>6.0</version>
</dependency>
    

log4j2.properties file:

appenders=console, rolling
rootLogger.level = debug
rootLogger.appenderRefs=console,rolling
rootLogger.appenderRef.console.ref=ConsoleAppender
rootLogger.appenderRef.rolling.ref=DailyLog
appender.console.type = Console
appender.console.name = ConsoleAppender
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5p] %d{dd-MM-yyyy HH:mm:ss} %l - %m%n
appender.rolling.type = RollingFile
appender.rolling.name = DailyLog
appender.rolling.fileName = ${catalina.base}/logs/myWebApp
appender.rolling.filePattern = ${catalina.base}/logs/myWebApp.%d{yyyy-MM-dd}
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%-5p] %d{dd-MM-yyyy HH:mm:ss} %l - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
    

Java class file:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.cesecore.util.CryptoProviderTools;

public class Certificate {
    private static final Logger LOG = LogManager.getLogger(Certificate.class);
    protected Certificate() {
        
        try{
            
            CryptoProviderTools.installBCProvider(); // <-- the exception is threw here
            ...
            ...
                        
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

What can I do so that CryptoProviderTools.installBCProvider() starts to work with log4j2?

4
  • 1
    This question is similar to: Mixing log4j 1.x and log4j 2. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented May 20 at 21:32
  • I think this case is different from the link. Maybe the CryptoProviderTools.installBCProvider() instruction is conditioned to log4j1 Commented May 20 at 21:57
  • I think this case is different from the link. Maybe the CryptoProviderTools.installBCProvider() instruction is conditioned to log4j1 Commented May 20 at 21:57
  • 1
    Either you modify the CryptoProviderTools (which is not described in the question) to use log4j 2 or you use the log4j 2 bridge so log4j 1 calls are redirected to log4j 2 (which is the method of the answer I shared). Commented May 21 at 2:41

1 Answer 1

0

You simply need to include the log4j-1.2-api into your pom.xml, Like this from log4j-maven.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.