4

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.

Matan D
  • 131
  • 10
  • 2
    As a temporary solution, I added "JAR Application" in IntelliJ configurations, and I run my application through the .jar file. – Matan D Jul 22 '19 at 08:26
  • 1
    Maybe this one contains a more insightful solution: https://stackoverflow.com/questions/34577936/spring-boot-devtools-causing-classcastexception-while-getting-from-cache – Yason Jan 25 '20 at 07:20

2 Answers2

4

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
Yason
  • 340
  • 2
  • 11
  • Why not simply flag this a duplicate question as the symptoms seem to be same. OP has not mentioned anything about using devtools. – Prashant Jan 25 '20 at 07:24
  • When I read the other answer originally, it did not make sense because it has to do with something other than IntelliJ. I don't think the question is a duplicate. – Yason Jan 27 '20 at 06:56
  • OK, this makes sense! Thanks for your explanation. – Prashant Jan 27 '20 at 07:48
  • Why not disabling IntelliJ class loader instead? In my case the same project - on Eclipse - works like a charm. – andreagalle Dec 15 '21 at 09:26
0

You can try clearing Intellij cache, that often helps with strange bugs like these.

If the problem persists, please check your run configuration and make sure that it's doing the same as java -jar my-app.jar command.

Šimon Kocúrek
  • 1,591
  • 1
  • 12
  • 22