Browse Source

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
pull/10447/merge
Andy Wilkinson 8 years ago
parent
commit
082258952a
  1. 22
      spring-boot-integration-tests/spring-boot-integration-tests-embedded-servlet-container/pom.xml
  2. 18
      spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java

22
spring-boot-integration-tests/spring-boot-integration-tests-embedded-servlet-container/pom.xml

@ -63,26 +63,4 @@ @@ -63,26 +63,4 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>java9</id>
<activation>
<jdk>9</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/EmbeddedServletContainerJarDevelopmentIntegrationTests.java</exclude>
<exclude>**/EmbeddedServletContainerWarDevelopmentIntegrationTests.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

18
spring-boot/src/main/java/org/springframework/boot/web/servlet/server/StaticResourceJars.java

@ -18,7 +18,9 @@ package org.springframework.boot.web.servlet.server; @@ -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 { @@ -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<URL> urls, URL url) {
try {
if ("file".equals(url.getProtocol())) {

Loading…
Cancel
Save