Search code examples
javaspring-bootexceptionintellij-ideaclassloader

Spring Boot App. running with Intellij leads to ClassCastException on the same classes


I have a Spring Boot Application which uses Jetty as Web Server, I also use gradle to build the application into executable .jar.

When I run my application with IntelliJ run or debug option, I suddenly get a class cast exception inside one of my controllers:

ClassCastException: com.test.matan.GenericParameter can not be cast into com.test.matan.GenericParameter

Its the same class as you can see. This class is a part of a dependency module of my Application, which I developed.

When I run my application with the executable jar: "java -jar my-app.jar" the same process works just fine, no exceptions and casting works fine as expected.

I came to a conclusion that IntelliJ is in charge of this strange "bug", but I don't understand how can I fix it. Running the application directly from the IDE is much more convenient and easier.

Any ideas? appreciated!

Edit: I use IntelliJ 2017 and can not update for now due to work's network limitations.


Solution

  • This is happening because spring-boot-devtools has a class loader, but IntelliJ has it's own class loader. Removing spring-boot-devtools from pom.xml solved this for me. Also you can just remove it from the libraries list in your project:

    Project Settings -> Libraries -> find spring-boot-devtools -> delete