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
pull/488/merge
Dave Syer 11 years ago
parent 08aacf72e0
commit a71c9b5de7

@ -73,8 +73,13 @@ public class LaunchedURLClassLoader extends URLClassLoader {
return urls[0]; return urls[0];
} }
} }
try {
return super.findResource(name); return super.findResource(name);
} }
catch (IllegalArgumentException e) {
return null;
}
}
@Override @Override
public Enumeration<URL> findResources(String name) throws IOException { public Enumeration<URL> findResources(String name) throws IOException {

@ -21,6 +21,7 @@ import java.net.URL;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/** /**
@ -28,6 +29,19 @@ import static org.junit.Assert.assertTrue;
*/ */
public class LaunchedURLClassLoaderTests { 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 @Test
public void resolveResourceFromArchive() throws Exception { public void resolveResourceFromArchive() throws Exception {
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL( LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(

Loading…
Cancel
Save