Skip to content

Instantly share code, notes, and snippets.

@Moddus
Created January 12, 2016 13:31
Show Gist options
  • Save Moddus/f3daff49031fba6dfb1a to your computer and use it in GitHub Desktop.
Save Moddus/f3daff49031fba6dfb1a to your computer and use it in GitHub Desktop.
public static File getFile(String name) {
URL url = Thread.currentThread().getContextClassLoader().getResource(name.replace(File.separatorChar, '/'));
if(url != null) {
try {
return new File(url.toURI());
} catch (Exception var3) {
throw new RuntimeException("Resource was found on classpath, but cannot be resolved to a normal file (maybe it is part of a JAR file): " + name);
}
} else {
File file = new File(name);
if(file.exists()) {
return file;
} else {
throw new RuntimeException("Cannot find resource in classpath or in file-system (relative to CWD): " + name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment