From 082258952ab3c7f4ab0bf3e440b169061973e7dd Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 29 Sep 2017 12:36:11 +0100 Subject: [PATCH] Fall back to JVM's class path when finding jars with static resources Previously, StaticResourceJars would only find jars with META-INF/resources content if it had been loaded by a URLClassLoader. This is not the case on Java 9 and, as a result, static content in META-INF/resources of any jars on the class path was not found. This commit updates StaticResourceJars to fall back to using the JVM's class path to find static resource jars when it was loaded by a ClassLoader that is not a URLClassLoader. Closes gh-10455 --- .../pom.xml | 22 ------------------- .../servlet/server/StaticResourceJars.java | 18 +++++++++++++++ 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/spring-boot-integration-tests/spring-boot-integration-tests-embedded-servlet-container/pom.xml b/spring-boot-integration-tests/spring-boot-integration-tests-embedded-servlet-container/pom.xml index 25264ca0688..59fb0a5da63 100644 --- a/spring-boot-integration-tests/spring-boot-integration-tests-embedded-servlet-container/pom.xml +++ b/spring-boot-integration-tests/spring-boot-integration-tests-embedded-servlet-container/pom.xml @@ -63,26 +63,4 @@ - - - java9 - - 9 - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - **/EmbeddedServletContainerJarDevelopmentIntegrationTests.java - **/EmbeddedServletContainerWarDevelopmentIntegrationTests.java - - - - - - - diff --git a/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java index 618649051e0..304b2557988 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java @@ -18,7 +18,9 @@ package org.springframework.boot.web.servlet.server; import java.io.File; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.net.JarURLConnection; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.net.URLConnection; @@ -43,9 +45,25 @@ class StaticResourceJars { addUrl(urls, url); } } + else { + for (String entry : ManagementFactory.getRuntimeMXBean().getClassPath() + .split(File.pathSeparator)) { + addUrl(urls, toUrl(entry)); + } + } return urls; } + private URL toUrl(String classPathEntry) { + try { + return new File(classPathEntry).toURI().toURL(); + } + catch (MalformedURLException ex) { + throw new IllegalArgumentException( + "URL could not be created from '" + classPathEntry + "'", ex); + } + } + private void addUrl(List urls, URL url) { try { if ("file".equals(url.getProtocol())) {