Last active
June 6, 2020 00:54
-
-
Save headius/32416a79faf14f63d660c40d83021bcf to your computer and use it in GitHub Desktop.
Example case for how URLClassLoader breaks the JarFile cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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