0

I have IntelliJ community 2020.2.4 version and a Spring Boot 2.7.10 web application (called main-app) using maven with openjdk version "11.0.18" 2023-01-17,

This application has a dependency which is another project (which compiled to a core.jar file).

I don't want to use Logback by default provided by Spring Boot, but log4j version 1. Hence:

  • In core project, with pom.xml, it has:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.2.5.RELEASE</version>
        </dependency>
  • In main-app project, with pom.xml, it has:
        <dependency>
            <groupId>org.test</groupId>
            <artifactId>core</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>            
        </dependency>    

Normally, what I do to be able to run and debug this main-app in my local system is, I run it in a Linux terminal:

cd core && mvn install >> /dev/null
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"

Then, I can use IntelliJ to attach remote debugger (although IntelliJ and Spring Boot application are both in a local system) like this:

enter image description here

However, I don't understand why, if I just run the main method in ApplicationMain.java in main-app application with Intellij, then it has error and Spring boot stopped (shift + F10).

enter image description here

ERROR [2023-06-23 04:52:05] ApplicationMain@712: Error starting petascope with embedded Tomcat. Reason: class org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to class org.apache.logging.log4j.core.LoggerContext (org.apache.logging.slf4j.SLF4JLoggerContext and org.apache.logging.log4j.core.LoggerContext are in unnamed module of loader 'app')
java.lang.ClassCastException: class org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to class org.apache.logging.log4j.core.LoggerContext (org.apache.logging.slf4j.SLF4JLoggerContext and org.apache.logging.log4j.core.LoggerContext are in unnamed module of loader 'app')
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.getLoggerContext(Log4J2LoggingSystem.java:401)
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.beforeInitialize(Log4J2LoggingSystem.java:154)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:238)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:220)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
    at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:79)
    at org.springframework.boot.SpringApplicationRunListeners.lambda$starting$0(SpringApplicationRunListeners.java:56)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
    at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:56)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:298)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
    at org.rasdaman.ApplicationMain.main(ApplicationMain.java:706)
    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.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

I don't understand what is wrong with Spring Boot here when IntelliJ tried to start it via the main method, while it works fine with mvn spring-boot:run.

Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50
  • Check if https://stackoverflow.com/a/59907162/12844632 looks similar to your case – Egor Klepikov Jun 23 '23 at 07:40
  • Try running the application via [Maven](https://www.jetbrains.com/help/idea/work-with-maven-goals.html#run_maven_goal_run_config) or [JAR](https://www.jetbrains.com/help/idea/run-debug-configuration-jar.html) run/debug configuration to see if it helps – Egor Klepikov Jun 23 '23 at 07:41
  • First if your IntelliJ version is of 2020.. your should upgrade ...also you are using a version `1.2.5.RELEASE` (`spring-boot-starter-log4j`) those versions are really out of date... use most recent version of IntellIJ 2023.X .. also use most recent version of spring boot 3.1+ – khmarbaise Jun 23 '23 at 09:15
  • @EgorKlepikov Thank you, but https://stackoverflow.com/a/59907162/12844632 doesn't work for me. I removed spring devtools from External Libraries. With this https://www.jetbrains.com/help/idea/2020.2/work-with-maven-goals.html#run_goal and use Maven window > project app-main > Plugins > spring-boot (comes from spring-boot-maven-plugin in pom.xml) > spring-boot:run AND this extra parameter -Dspring-boot.run.fork=false then I can see IntellJ can start Spring boot application with debugger. But this is not good as devtools doesn't work with fork=false. – Bằng Rikimaru Jun 23 '23 at 10:13

1 Answers1

0

This works well with IntelliJ and spring boot devtools, IF, I removed Log4j from pom.xml for both core and main-app applications and I don't add this exclusions anymore:

            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>    

In application.properties of main-app, I have:

spring.devtools.restart.additional-paths=../core

Then, with Logback the default logging of Spring boot which I tested and I can just run ApplicationMain in main-app via Intellj without any problem.

It is very nice when I changed something in core application and IntelliJ can allow Spring devtools to classes in classpath and in main-app the result is changed as well.

Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50