shaan bhattacharya
shaan bhattacharya

Reputation: 213

Java Class cast Exception - Spring boot

Exception in thread "main" java.lang.ClassCastException: java.base/jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to java.base/java.net.URLClassLoader

 at org.springframework.boot.devtools.restart.DefaultRestartInitializer.getUrls(DefaultRestartInitializer.java:93)
 at org.springframework.boot.devtools.restart.DefaultRestartInitializer.getInitialUrls(DefaultRestartInitializer.java:56)
 at org.springframework.boot.devtools.restart.Restarter.<init(Restarter.java:138)
 at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:537)
 at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationStartedEvent(RestartApplicationListener.java:68)
 at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationEvent(RestartApplicationListener.java:45)
 at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)
 at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)
 at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:121)
 at org.springframework.boot.context.event.EventPublishingRunListener.started(EventPublishingRunListener.java:63)
 at org.springframework.boot.SpringApplicationRunListeners.started(SpringApplicationRunListeners.java:48)
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186)
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175)
 at com.rme.hub.RmeApplication.main(RmeApplication.java:24)

Upvotes: 16

Views: 102275

Answers (4)

Kidus Tekeste
Kidus Tekeste

Reputation: 661

Check your JAVA_HOME in hadoop-env.sh. Changing my JAVA_HOME value in $HADOOP_HOME/etc/hadoop with java 8 (export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64) value in the hadoop-env.sh solved this issue for me. I had a higher java version defined in my hadoop-env.sh value when I had defined java 8 in my .bashrc file. So keeping both the same java 8 version fixed the issue for me.

Upvotes: 0

Hieu Nguyen
Hieu Nguyen

Reputation: 61

I had this problema on eclipse and had solved by doing these step:

  • First download java 8 (If you don't have) then install it
  • Second, I am not sure if you have to do it or not but just do it. I create JAVA_HOME path by right click to my desktop -> properties -> Advanced System Setting -> Advanced Tab -> Environment Variables -> Add JAVA_HOME in both User Variables and System Variables -> Click new -> Variable Home is "JAVA_HOME", Variable Value can be left empty -> Browse Directory -> point to your jdk folder (Exmaple: mine is C:\Program Files\Java\jdk1.8.0_291)
  • Third, go to eclipse -> go to window tab -> choose java on the menu on the left side -> Installed JREs -> in default eclipse already had jre directory -> click to it and then click duplicate -> change the directory to your own jre directory (example: C:\Program Files\Java\jre1.8.0_291)
  • Forth: back to java menu, choose compiler option -> then on the right side -> change compiler compliance level to 1.8.

That is all! Hope it will be useful for you.

Upvotes: 4

Prasun Patidar
Prasun Patidar

Reputation: 1


solution: change JDK version 8 in your IDE



I got the same issue in the spring boot application in IntelliJ idea and sts but I found one solution in that issue :

if you are using AD integration in your project with it belongs to Microsoft, then you need to use java 8 version JDK because currently Microsoft AD plugins are not supported to java 9 or higher version it will support only the java 8 or lower version

Upvotes: 0

Andy Wilkinson
Andy Wilkinson

Reputation: 116111

Judging by the presence of java.base/jdk.internal.loader.ClassLoaders in the stack trace, you are using Java 9 or later. Spring Boot's DefaultRestartInitializer is trying to cast the app class loader to a URLClassLoader. This works in Java 8 and earlier but does not work with Java 9 or later. Spring Boot had been updated in 2.0 to cope with this change in Java 9.

If you want to use Spring Boot with Java 9 or later, you should upgrade to Spring Boot 2. At the time of writing, the latest release is 2.0.5 which supports Java 8, 9, and 10. Spring Boot 2.1, which will be released later this year, will add support for Java 11.

Upvotes: 49

Related Questions