NoClassDefFoundError after IntelliJ IDEA upgrade

Recently I created a new project using IntelliJ IDEA and Gradle for dependency management. After resolving all the dependencies needed by the project and running it I get the following error:

java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
    at com.some.fancy.name.Application.main(Application.java:13)
    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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
    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)
    ... 6 more

The problem occurs only with the classes from the resolved dependencies (I tried it also with a simple main program using Cache from Guava). The class causing the error is the following:

@SpringBootApplication
public class Application {
    public static void main(String... args) {
        SpringApplication.run(Application.class, args);
    }
}

All the fields are shown correct colour (annotations are yellow, nothing is red), IntelliJ has all the resolved dependencies in the External Libraries directory. I'm using the green "run" arrow to start the project.

When using gradle from the command line, creating a fat jar then everything works fine. I already tried "Invalidate Caches/Restart", pulled the project several times from the repository, cleared the gradle caches, removed all the dependencies by hand, tried to run it using the Community Edition, the Ultimate Edition, running older projects which already worked some time ago (and don't work now).

Could it be somehow related to updating IntelliJ IDEA to 2016.3.4, build 163.12024.16?


Solution 1:

It's a known issue in IntelliJ IDEA that is specific to Gradle 3.4 and later versions:

  • IDEA-167412 Gradle 3.4-rc-1 changes compile dependencies to provided
  • original bug report in the Gradle project with more details
  • comment from the responsible developer regarding "Create Module per source set" option and how Gradle integration works in IntelliJ IDEA

It's already fixed in IntelliJ IDEA 2017.1 and 2016.3.6.

You can also use Gradle 3.3 or older as a workaround if you can't or don't want to update IntelliJ IDEA.

Solution 2:

I had the same problem, getting the error below from a spring boot application which was working fine a day before. I am using IntelliJ 2020.3, Gradle 6.5.1. I suspect my repository might have been corrupted.

Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication

After lot of tries from my IntelliJ I tried bootRun (in the gradle tree you can find this under Tasks > application > bootRun) and it worked.