java.base/jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to java.base/java.net.URLCl

在设置项目的热部署时,需要添加对 spring-boot-devtools 的依赖,因为没有给定版本号,maven默认添加的是 v 1.5.8 版本。当时安装JDK时,看到最新的 jdk-1.9, 就顺手安装了最新版本的JDK. 但是添加依赖之后,项目启动失败,报如下异常:

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.(Restarter.java:140)
	at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:546)
	at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationStartingEvent(RestartApplicationListener.java:67)
	at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationEvent(RestartApplicationListener.java:45)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
	at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:69)
	at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:292)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
	at com.haiyoung.hyweb.HyWebApplication.main(HyWebApplication.java:23)

这个异常是因为老版本的 spring-boot-devtools 与最新版的 JDK之间不兼容,在网上找了找资料,关于这方面的资料没看到什么东西,上stackoverflow上找了找,也不多,就找到一个相关的,链接如下:

https://stackoverflow.com/questions/39739075/hive-shell-not-loading/41637409#41637409

大致描述如下:

In Java 9 “Application and extension class loaders are no longer instances of java.net.URLClassLoader”, see “Prepare for JDK 9”, Alan Bateman, 
Oct 2015: http://openjdk.java.net/projects/jigsaw/talks/prepare-for-jdk9-j1-2015.pdf. I'm not sure where exactly the problem lies, if it's in httpunit 
itself or the JSP compiler libraries, but you might want to run some Java 9 tests yourself.

Application and extension class loaders are no longer instances of java.net.URLClassLoader
意思是说,在 java 9中,应用程序和扩展类都不再是 java.net.URLClassLoader 的实例。将 spring-boot-devtools 版本换成 v2.0.0.M5 重新启动项目,异常消失,项目重新启动。不过为了避免后面太多坑,果断将JDK版本回退到 1.8,回退之后,spring-boot-devtools v1.5.8时,项目正常启动,restart 和 livereload也设置成功,正常使用。

网上也有一些其他的解决方案,不过懒得折腾,以后遇到再看。







你可能感兴趣的:(Java,Spring,Java,EE,springboot)