16
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)
3
  • 3
    Can you be more specific with your question? Commented Sep 18, 2018 at 11:33
  • 3
    With just the stack trace is impossible to answer. Sharing your code would make it easier Commented Sep 18, 2018 at 11:52
  • Despite the departure from the rules, comments and the closure of the question, the question for those who are in the same situation is clear. Thank you author! Commented May 26, 2020 at 16:52

4 Answers 4

49

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Description: Field userRepo in com.rme.hub.service.UserService required a bean named 'entityManagerFactory' that could not be found. Action: Consider defining a bean named 'entityManagerFactory' in your configuration.
That appears to be a completely unrelated error. If this question has allowed you to make some progress then I'd recommend moving on and asking another question. If you do go on to ask another question, I'd also recommend reading and acting upon this advice first.
I am using SpringBoot4 and jdk11. But still I am getting same error. When I commented out DevTools from pom.xml. Then it's working. Why can't i use devtools here ?
@SritamJagadev I guess SpringBoot4 is a typo? At the time of writing, the latest version is 2.1.3. You need to be using 2.0 or later to use Java 9 or later. Support for JDK 11 was officially added in Spring Boot 2.1.
That worked for me in Intellij and set edited my build to use an older java version (Java 8) and the build worked fine.
4

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.

1 Comment

Downgrading to Java8 is generally not a wise approach. You should instead consider upgrading your SprintBoot.
0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.