diff --git a/src/main/java/org/jmxtrans/agent/util/io/ClasspathResource.java b/src/main/java/org/jmxtrans/agent/util/io/ClasspathResource.java index 39ec50c1..6e41d3eb 100644 --- a/src/main/java/org/jmxtrans/agent/util/io/ClasspathResource.java +++ b/src/main/java/org/jmxtrans/agent/util/io/ClasspathResource.java @@ -26,6 +26,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.net.URL; import javax.annotation.Nonnull; @@ -59,13 +60,22 @@ public boolean exists() { @Nonnull @Override public URL getURL() { - return classLoader.getResource(path); + URL resource = classLoader.getResource(path); + if (resource == null) { + throw new NullPointerException("No resource '" + path + "' found in classloader " + classLoader); + } + return resource; } @Nonnull @Override public File getFile() { - return new File(getURI()); + URI uri = getURI(); + try { + return new File(uri); + } catch (RuntimeException e) { + throw new FileNotFoundRuntimeException("Resource '" + uri + "' can not be resolved as a file", e); + } } @Nonnull diff --git a/src/main/java/org/jmxtrans/agent/util/io/IoUtils.java b/src/main/java/org/jmxtrans/agent/util/io/IoUtils.java index e7238b26..62f8050f 100644 --- a/src/main/java/org/jmxtrans/agent/util/io/IoUtils.java +++ b/src/main/java/org/jmxtrans/agent/util/io/IoUtils.java @@ -152,7 +152,7 @@ public static Document getFileAsDocument(@Nonnull Resource resource) throws IoRu try { File configurationFile = resource.getFile(); return dBuilder.parse(configurationFile); - } catch(FileNotFoundRuntimeException e) { + } catch(IoRuntimeException e) { try (InputStream in = resource.getInputStream()) { return dBuilder.parse(in); }