Add escape hatch for ClassLoader.findResource() for invalid path
The source of the exception is in sun.misc (so hard to track down precisely) but it's clear that the LaunchedJarURLClassLoader needs to be more defensive and return null from findResource() if it can't find it. Fixes gh-486
This commit is contained in:
parent
08aacf72e0
commit
a71c9b5de7
|
@ -73,8 +73,13 @@ public class LaunchedURLClassLoader extends URLClassLoader {
|
|||
return urls[0];
|
||||
}
|
||||
}
|
||||
try {
|
||||
return super.findResource(name);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<URL> findResources(String name) throws IOException {
|
||||
|
|
|
@ -21,6 +21,7 @@ import java.net.URL;
|
|||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
|
@ -28,6 +29,19 @@ import static org.junit.Assert.assertTrue;
|
|||
*/
|
||||
public class LaunchedURLClassLoaderTests {
|
||||
|
||||
@Test
|
||||
public void resolveResourceFromWindowsFilesystem() throws Exception {
|
||||
// This path is invalid - it should return null even on Windows.
|
||||
// A regular URLClassLoader will deal with it gracefully.
|
||||
assertNull(getClass().getClassLoader().getResource(
|
||||
"c:\\Users\\user\\bar.properties"));
|
||||
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(
|
||||
"jar:file:src/test/resources/jars/app.jar!/") }, getClass()
|
||||
.getClassLoader());
|
||||
// So we should too...
|
||||
assertNull(loader.getResource("c:\\Users\\user\\bar.properties"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveResourceFromArchive() throws Exception {
|
||||
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(
|
||||
|
|
Loading…
Reference in New Issue