build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.0'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
        // exclude logback logging
        compile.exclude module: 'spring-boot-starter-logging'
}

dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-actuator'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-log4j2'
        ...
}

Build and start are OK, but an error occurs when the following command is executed. java -Djarmode=layertools -jar application.jar extract

log

INFO[0148] Running: [/bin/sh -c java -Djarmode=layertools -jar application.jar extract] 
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.ExceptionInInitializerError
    at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)
    at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
    at org.springframework.core.io.support.SpringFactoriesLoader.<clinit>(SpringFactoriesLoader.java:71)
    at org.springframework.boot.loader.jarmode.JarModeLauncher.main(JarModeLauncher.java:39)
    ... 8 more
Caused by: java.lang.NullPointerException
    at org.springframework.core.io.support.SpringFactoriesLoader.loadSpringFactories(SpringFactoriesLoader.java:136)
    at org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(SpringFactoriesLoader.java:132)
    at org.springframework.core.io.support.SpringFactoriesLoader.loadFactories(SpringFactoriesLoader.java:101)
    at org.springframework.boot.logging.LoggingSystemFactory.lambda$fromSpringFactories$0(LoggingSystemFactory.java:44)
    at org.springframework.boot.logging.DelegatingLoggingSystemFactory.getLoggingSystem(DelegatingLoggingSystemFactory.java:41)
    at org.springframework.boot.logging.LoggingSystem.get(LoggingSystem.java:159)
    at org.springframework.boot.logging.log4j2.SpringBootConfigurationFactory.getConfiguration(SpringBootConfigurationFactory.java:60)
    at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:551)
    at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:475)
    at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:323)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:687)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:708)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:263)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
    at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:155)
    ... 14 more

Comment From: snicoll

Thanks for the report but a partial snippet from your build is not helpful. Can you please attach a small sample that we can run to reproduce this problem? You can do so by attaching a zip to this issue or sharing a link to a GitHub repository.

Comment From: underbell

Thanks for responding. This is a demo in which only logging is changed to log4j2 in Spring Initializr. github : https://github.com/underbell/log4j2-demo

It does not occur in the case of logback. Occurs in the case of log4j2.

Comment From: snicoll

@underbell thank you for the sample and sharing additional details. This is a regression in the way we lookup which LoggingSystem to use on startup.

You can workaround the issue for now by adding an extra system property, i.e.:

java -Djarmode=layertools -Dorg.springframework.boot.logging.LoggingSystem=none -jar application.jar

Comment From: underbell

@snicoll Thanks for your guide.