25

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?

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
Adrian Jałoszewski
  • 1,695
  • 3
  • 17
  • 33

3 Answers3

33

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

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.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 2
    Just a note in case others run into the same problem. This is not specific to Gradle 3.4 as the problem still occurs with 3.5. The fix does work though (updating IntelliJ) so the solution stands. – Hugo Migneron Jun 09 '17 at 19:04
  • 2
    I am facing the same problem. In my case, I can run it when I compile it with IntelliJ/Maven and run it through the terminal (java -jar myapp.jar). But when I try to run it from IntelliJ menu, it goes to this error. Also, if I run it with Eclipse, everything runs fine. Very strange! IDEA is very unstable! This is my conclusion after using it more than 2 years, unfortunatelly. – Raffael Bechara Rameh Feb 01 '18 at 11:47
  • 4
    Same problem again with 2017.3 :( – Stefan Haustein Feb 26 '18 at 20:41
  • @StefanHaustein it's not the same problem then, please report at https://youtrack.jetbrains.com/issues/IDEA – CrazyCoder Feb 26 '18 at 21:04
  • p.s. I have tried to switch my gradle wrapper back to gradle 3.3 (also to see if this is a regression), but gradle 3.3. doesn't support google(), so this doesn't seem to be an option for cross platform projects any longer... – Stefan Haustein Feb 27 '18 at 14:09
1

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.

Graham Asher
  • 1,648
  • 1
  • 24
  • 34
akarahman
  • 235
  • 2
  • 15
0

Setting gradle version to 3.3 corrected this issue.

Mrityunjaya
  • 83
  • 2
  • 12