Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example case for how URLClassLoader breaks the JarFile cache
import java.net.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* To run, first create a jar file:
*
* $ touch blah.txt
* $ jar cf blah.jar blah.txt
* $ rm blah.txt
*/
public class BrokenURLClassLoader {
public static final int PARALLEL = 10;
public static void main(String[] args) throws Throwable {
URL[] urls = {new URL("jar:file:blah.jar!/")};
String filename = "blah.txt";
for (int iter = 0; ; iter++) {
List<Throwable> failures = IntStream.rangeClosed(1, PARALLEL)
.parallel()
.mapToObj(i -> {
try {
URLClassLoader urlc = new URLClassLoader(urls);
urlc.getResourceAsStream(filename).close();
urlc.close();
} catch (Throwable t) {
return t;
}
return null;
})
.filter(t -> t != null)
.collect(Collectors.toList());
if (failures.size() != 0) {
System.err.println("failures on iteration " + iter);
failures.forEach(t -> t.printStackTrace());
System.exit(1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment