From 7103eab2e6d2b71a4ae2ea9c14c709d16fcc1963 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 12 Jun 2019 11:32:24 +0100 Subject: [PATCH] Close JarFiles when looking for Class-Path manifest entries Fixes gh-17095 --- .../boot/devtools/restart/ChangeableUrls.java | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java index d51d1b54f7..dd42b5ce09 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java @@ -117,29 +117,24 @@ final class ChangeableUrls implements Iterable { } private static List getUrlsFromClassPathOfJarManifestIfPossible(URL url) { - JarFile jarFile = getJarFileIfPossible(url); - if (jarFile == null) { - return Collections.emptyList(); - } - try { - return getUrlsFromManifestClassPathAttribute(url, jarFile); - } - catch (IOException ex) { - throw new IllegalStateException("Failed to read Class-Path attribute from manifest of jar " + url, ex); - } - } - - private static JarFile getJarFileIfPossible(URL url) { try { File file = new File(url.toURI()); if (file.isFile()) { - return new JarFile(file); + try (JarFile jarFile = new JarFile(file)) { + try { + return getUrlsFromManifestClassPathAttribute(url, jarFile); + } + catch (IOException ex) { + throw new IllegalStateException( + "Failed to read Class-Path attribute from manifest of jar " + url, ex); + } + } } } catch (Exception ex) { // Assume it's not a jar and continue } - return null; + return Collections.emptyList(); } private static List getUrlsFromManifestClassPathAttribute(URL jarUrl, JarFile jarFile) throws IOException {