当我将 Spring Boot 开发者工具添加到 Gradle 构建中时,我有一个 Spring Boot 应用程序无法在 IntelliJ 2017.1.5 中启动,如下所示:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.apache.httpcomponents:httpclient:4.5.3")
compile("com.fasterxml.jackson.core:jackson-databind:2.8.8.1")
compile("commons-validator:commons-validator:1.6")
// This line breaks IntelliJ compatibility:
optional("org.springframework.boot:spring-boot-devtools")
optional("org.springframework.boot:spring-boot-configuration-processor")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("junit:junit:4.12")
}
如果没有
spring-boot-devtools
一切都很好,一旦添加它们,启动应用程序(在 IntelliJ 中刷新 Gradle 项目后)就会失败,并显示 NoClassDefFoundError
:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/web/client/RestTemplateBuilder
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.web.client.RestTemplateBuilder
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
我很确定这是一个 IntelliJ 问题,因为在启用了 Spring Boot 开发人员工具的情况下,相同的应用程序可以在 Eclipse 中正常运行。
有什么想法可以解决这个问题吗?
我能够通过将
spring-boot-devtools
的依赖关系更改为: 来解决此问题
runtime("org.springframework.boot:spring-boot-devtools")
而不是
optional("org.springframework.boot:spring-boot-devtools")
就我而言,问题在于
main.iml
InteaalijIdea 生成的。 特别是我改变了
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
到
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />