Intellij 运行相同的 Spring Boot 项目,社区版运行良好,但终极版抛出错误

Posted

技术标签:

【中文标题】Intellij 运行相同的 Spring Boot 项目,社区版运行良好,但终极版抛出错误【英文标题】:Intellij to run the same Spring Boot project, Community version worked well but Ultimate version threw errors 【发布时间】:2019-10-21 11:47:15 【问题描述】:

我用 Spring Boot 编写了最简单的 hello world 项目。也就是说,当你在浏览器中打开 localhost:8080 时,你会看到“hello world”字样。我刚刚发现,如果我用 Intellij Community 运行项目,IDE 可以启动项目,我可以在浏览器中打开 localhost:8080。但是,如果我在 Intellij Ultimate 中运行相同的项目,它会向我抛出错误。

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-android-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-jcl-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-jdk14-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-nop-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-simple-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/xtynd/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.AndroidLoggerFactory]
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.AndroidLoggerFactory loaded from file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-android-1.7.25.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.AndroidLoggerFactory
    at org.springframework.util.Assert.instanceCheckFailed(Assert.java:655)
    at org.springframework.util.Assert.isInstanceOf(Assert.java:555)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:286)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:102)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:219)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:198)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
    at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:69)
    at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:302)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at io.firstspringboot.SpringApp.main(SpringApp.java:9)

Process finished with exit code 1

我使用的是完全相同的 pom.xml。这两个 IDE 有什么区别?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.firstspringboot</groupId>
    <artifactId>rest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

添加于美国东部时间 6 月 6 日上午 11 点

感谢大家的cmets和答案。我忘了提及我所做的一些努力。根据this stackflow question 的答案等一些研究,我试图在我的 pom.xml 文件中排除这样的内容,以消除 Intellij Ultimate 中的 LoggerFactory 错误:

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

然后Intellij Ultimate又给我报错了,Intellij Community又可以通过点击“运行”按钮完美启动项目:

"C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\lib\idea_rt.jar=51923:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_191\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jcl-over-slf4j-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jcl-over-slf4j-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jul-to-slf4j-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jul-to-slf4j-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\log4j-over-slf4j-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\log4j-over-slf4j-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\osgi-over-slf4j-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\osgi-over-slf4j-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-android-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-android-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-api-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-api-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-ext-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-ext-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-jcl-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-jcl-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-jdk14-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-jdk14-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-log4j12-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-log4j12-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-migrator-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-nop-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-nop-1.7.25.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-simple-1.7.25-sources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\slf4j-simple-1.7.25.jar;C:\JavaProj\quickstart\FirstSpringBoot\target\classes;C:\Users\xtynd\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.1.5.RELEASE\spring-boot-starter-web-2.1.5.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\springframework\boot\spring-boot-starter\2.1.5.RELEASE\spring-boot-starter-2.1.5.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\springframework\boot\spring-boot\2.1.5.RELEASE\spring-boot-2.1.5.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.1.5.RELEASE\spring-boot-autoconfigure-2.1.5.RELEASE.jar;C:\Users\xtynd\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\Users\xtynd\.m2\repository\org\springframework\spring-core\5.1.7.RELEASE\spring-core-5.1.7.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\springframework\spring-jcl\5.1.7.RELEASE\spring-jcl-5.1.7.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;C:\Users\xtynd\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.1.5.RELEASE\spring-boot-starter-json-2.1.5.RELEASE.jar;C:\Users\xtynd\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.9.8\jackson-databind-2.9.8.jar;C:\Users\xtynd\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\Users\xtynd\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.9.8\jackson-core-2.9.8.jar;C:\Users\xtynd\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.8\jackson-datatype-jdk8-2.9.8.jar;C:\Users\xtynd\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.8\jackson-datatype-jsr310-2.9.8.jar;C:\Users\xtynd\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.8\jackson-module-parameter-names-2.9.8.jar;C:\Users\xtynd\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.1.5.RELEASE\spring-boot-starter-tomcat-2.1.5.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.19\tomcat-embed-core-9.0.19.jar;C:\Users\xtynd\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.19\tomcat-embed-el-9.0.19.jar;C:\Users\xtynd\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.19\tomcat-embed-websocket-9.0.19.jar;C:\Users\xtynd\.m2\repository\org\hibernate\validator\hibernate-validator\6.0.16.Final\hibernate-validator-6.0.16.Final.jar;C:\Users\xtynd\.m2\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;C:\Users\xtynd\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\xtynd\.m2\repository\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;C:\Users\xtynd\.m2\repository\org\springframework\spring-web\5.1.7.RELEASE\spring-web-5.1.7.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\springframework\spring-beans\5.1.7.RELEASE\spring-beans-5.1.7.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\springframework\spring-webmvc\5.1.7.RELEASE\spring-webmvc-5.1.7.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\springframework\spring-aop\5.1.7.RELEASE\spring-aop-5.1.7.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\springframework\spring-context\5.1.7.RELEASE\spring-context-5.1.7.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\springframework\spring-expression\5.1.7.RELEASE\spring-expression-5.1.7.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\springframework\boot\spring-boot-starter-log4j2\2.1.5.RELEASE\spring-boot-starter-log4j2-2.1.5.RELEASE.jar;C:\Users\xtynd\.m2\repository\org\apache\logging\log4j\log4j-slf4j-impl\2.11.2\log4j-slf4j-impl-2.11.2.jar;C:\Users\xtynd\.m2\repository\org\slf4j\slf4j-api\1.7.26\slf4j-api-1.7.26.jar;C:\Users\xtynd\.m2\repository\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;C:\Users\xtynd\.m2\repository\org\apache\logging\log4j\log4j-core\2.11.2\log4j-core-2.11.2.jar;C:\Users\xtynd\.m2\repository\org\apache\logging\log4j\log4j-jul\2.11.2\log4j-jul-2.11.2.jar;C:\Users\xtynd\.m2\repository\org\slf4j\jul-to-slf4j\1.7.26\jul-to-slf4j-1.7.26.jar" io.firstspringboot.SpringApp
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-android-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-jcl-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-jdk14-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-nop-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Program%20Files/Java/jdk1.8.0_191/jre/lib/slf4j-simple-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/xtynd/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.AndroidLoggerFactory]
Exception in thread "main" java.lang.NoClassDefFoundError: android/util/Log
    at org.slf4j.impl.AndroidLoggerAdapter.isLoggable(AndroidLoggerAdapter.java:543)
    at org.slf4j.impl.AndroidLoggerAdapter.isErrorEnabled(AndroidLoggerAdapter.java:447)
    at org.apache.commons.logging.impl.SLF4JLog.isErrorEnabled(SLF4JLog.java:63)
    at org.springframework.boot.SpringApplication.reportFailure(SpringApplication.java:857)
    at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:832)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at io.firstspringboot.SpringApp.main(SpringApp.java:9)

Process finished with exit code 1

我不确定的是,通过使用完全相同的项目,为什么一个 IDE 运行良好,而另一个 IDE 总是给我错误?

【问题讨论】:

你到底为什么把所有这些 SLF4J jar 和更多东西放在jre/lib 目录中?那会破坏事情。不要在里面放额外的罐子。 【参考方案1】:

再次感谢所有 cmets 和回复。经过相当多的努力,我终于找到了解决方案。事实证明,SDK 设置有点不同。我受到了有关 SFL4J 的 cmets 的启发。

首先,我打开文件 -> 项目结构 -> 项目设置 -> 项目,检查我用于运行项目的 SDK 名称。

其次,在同一个 Project Structure 窗口中,切换到 Platform Settings -> SDKs,然后单击我用来运行项目的 SDK。

即使在两个 IDE 中,我的 SDK 都设置为相同的 JDK 主路径(即 JAVA_HOME),但由于某些原因,两个 IDE 之间的 .jar 列表是不同的。 Ultimate IDE 中的列表比 Community IDE 中的列表长。作为一个 Java 新手,我无法说出这些列表不同的确切原因。我最好的猜测是,Community IDE 是在安装 Java 之后立即安装的,而 Ultimate IDE 是在我用 Java 运行了几个月后安装的。我通过在 Community IDE 中创建另一个 SDK 来验证这一点,该 SDK 具有与 Ultimate IDE 中相同的 .jar 列表,然后单击“运行”。现在,Community IDE 中的项目遇到了与我在 Ultimate IDE 遇到的问题中发布的相同的错误。

作为解决方案,我从 Intellij Ultimate 的 SDK 中删除了额外的 .jar 文件。它运作良好。无需向 pom.xml 添加排除项。我将发布 .jar 文件的原始列表供您参考。

没有抛出错误的 Intellij 社区 SDK:

还有给我 LoggerFactory 错误的 Intellij Ultimate SDK:

接下来我要调查的是那些删除的额外 .jar 文件对运行 Spring Boot 的影响。我想知道哪些 .jar 文件/依赖项会影响 Spring Boot 的运行。

【讨论】:

以上是关于Intellij 运行相同的 Spring Boot 项目,社区版运行良好,但终极版抛出错误的主要内容,如果未能解决你的问题,请参考以下文章

进程以退出代码 1 Spring Boot Intellij 结束

Intellij:运行配置Spring Boot项目

Intellij 运行配置 Spring Boot 与 Maven 问题

如何让 Intellij 中的 Spring MVC 运行 HTML

当 spring-boot 运行时 IntelliJ 进程以退出代码 0 完成

如何从 IntelliJ 运行/部署 Spring Boot 微服务到本地 Kubernetes 集群?