Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create class org.apache.logging.log4j.core.impl.Log4jContextFactory

I am using spring boot application for a project and getting the following error while running the project from jar using java -jar build/libs/com.user.admin.rest.services.jar. But its not a blocker and i was able to access the api's, and the logger is working fine as intended. I just want to resolve this error.

ERROR StatusLogger Unable to create class org.apache.logging.log4j.core.impl.Log4jContextFactory specified in jar:file:/Users/Samuel/UserMgmt/com.user.admin.rest.services/build/libs/com.user.admin.rest.services.jar!/META-INF/log4j-provider.properties
 java.lang.ClassNotFoundException: org.apache.logging.log4j.core.impl.Log4jContextFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.apache.logging.log4j.spi.Provider.loadLoggerContextFactory(Provider.java:96)
    at org.apache.logging.log4j.LogManager.<clinit>(LogManager.java:91)
    at com.user.admin.rest.services.api.UserServiceResource.<clinit>(UserServiceResource.java:32)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
    at com.user.admin.rest.services.api.Application.main(Application.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

I have added the following lines for logger in the build.gradle file.

compile 'org.apache.logging.log4j:log4j-api:2.8.2' compile 'org.apache.logging.log4j:log4j-core:2.8.2'

And my log4j2.xml file is given below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="ERROR" name="example-config">
    <Properties>
        <Property name="PATTERN">%-5level %d [%t] %c:%M(%L): %m%n</Property>
    </Properties>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="${PATTERN}" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="TRACE">
            <AppenderRef ref="STDOUT" />
        </Root>
    </Loggers>
</Configuration>
like image 299
ysr Avatar asked Jul 05 '17 10:07

ysr


2 Answers

Generally speaking, you should never need to switch logging implementations; Logback should suit you fine. However, if you decide that you’d rather use Log4j or Log4j2, you’ll need to change your dependencies to include the appropriate starter for the logging implementation you want to use and to exclude Logback.
Spring Boot in Action

Swapping out Logback for Log4j2 in Gradle

  1. Exclude the default logging
configurations {
  all*.exclude group:'org.springframework.boot',
               module:'spring-boot-starter-logging'
}
  1. Add Log4j2 logging
compile("org.springframework.boot:spring-boot-starter-log4j2")

You can find details of spring-boot-starter-log4j2 dependency version that is provided by Spring Boot here.

like image 173
Boris Avatar answered Nov 09 '22 11:11

Boris


I have met the similar problem. The version of log4j-api and log4j-core have to match (and ClassNotFound is a strange exception here). As you have said in comment, you have found log4j-core-2.7.jar, but I may guess your log4j-api is in version of 2.8.2 as your above description shows.

Check your dependency, remove old version of log4j-core and add new version should fix the problem.

like image 45
Tony Avatar answered Nov 09 '22 10:11

Tony