Skip to content

Commit a31180d

Browse files
committed
Avoid calling URL.getContent() when defining a package
URL.getContent() is shorthand for URL.openConnection().getContent(). It creates an InputStream that isn't explicitly closed. This means that a file handle remains open until the URLConnection is garbage collected. This can lead to the process exceeding the limit for open files. Previously, LaunchedURLClassLoader was using getConent() when proactively defining a package for a class that is about to be loaded. getContent() was used to access nested jar files to check if they contained the package and, if so, to retrieve the jar's manifest. In place of using getContent(), this commit uses JarURLConnection's getJarFile() method which provides access to the JarFile without the unwanted side-effect of opening an input stream. Closes gh-7180
1 parent 24f8c73 commit a31180d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import java.security.AccessController;
2525
import java.security.PrivilegedExceptionAction;
2626
import java.util.Enumeration;
27+
import java.util.jar.JarFile;
2728

2829
import org.springframework.boot.loader.jar.Handler;
29-
import org.springframework.boot.loader.jar.JarFile;
3030

3131
/**
3232
* {@link ClassLoader} used by the {@link Launcher}.
@@ -131,8 +131,10 @@ public Object run() throws ClassNotFoundException {
131131
String classEntryName = className.replace(".", "/") + ".class";
132132
for (URL url : getURLs()) {
133133
try {
134-
if (url.getContent() instanceof JarFile) {
135-
JarFile jarFile = (JarFile) url.getContent();
134+
URLConnection connection = url.openConnection();
135+
if (connection instanceof JarURLConnection) {
136+
JarFile jarFile = ((JarURLConnection) connection)
137+
.getJarFile();
136138
if (jarFile.getEntry(classEntryName) != null
137139
&& jarFile.getEntry(packageEntryName) != null
138140
&& jarFile.getManifest() != null) {
@@ -175,8 +177,8 @@ public void clearCache() {
175177

176178
private void clearCache(URLConnection connection) throws IOException {
177179
Object jarFile = ((JarURLConnection) connection).getJarFile();
178-
if (jarFile instanceof JarFile) {
179-
((JarFile) jarFile).clearCache();
180+
if (jarFile instanceof org.springframework.boot.loader.jar.JarFile) {
181+
((org.springframework.boot.loader.jar.JarFile) jarFile).clearCache();
180182
}
181183
}
182184

0 commit comments

Comments
 (0)