Java Utililty Methods Resource Path Get

List of utility methods to do Resource Path Get

Description

The list of methods to do Resource Path Get are organized into topic(s).

Method

FilegetResourceFile(Class clazz, String relPath)
get Resource File
String uri = clazz.getResource(clazz.getSimpleName() + ".class").toString();
String result = clazz.getResource(clazz.getSimpleName() + ".class").toURI().getPath();
File file;
if (result != null && result.indexOf("/bin/") != -1) {
    file = new File(result.substring(0, result.indexOf("/bin/")) + relPath);
} else if (uri.indexOf("!/") != 0 && uri.startsWith("jar:file:")) {
    relPath = relPath.replace("/bin/", "/");
    uri = uri.substring("jar:file:".length());
...
FilegetResourceFile(final Class baseClass, final String path)
get Resource File
return new File(getResource(baseClass, path).toURI());
FilegetResourceFile(String sResourcePath, Class cRefClass)
Returns the given resource as a file.
URL url = cRefClass.getResource(sResourcePath);
if (url == null) {
    return null;
String filePath = url.getFile();
if (filePath == null) {
    return null;
filePath = filePath.replaceAll("%20", " ");
return new File(filePath);
StringgetResourceFilePath(String name)
get Resource File Path
ClassLoader loader = ClassLoader.getSystemClassLoader();
URL url = loader.getResource(name);
if (url == null) {
    throw new RuntimeException("File does not exist: " + name);
return url.getFile();
FilegetResourceFileRelativeToBase(final File baseDir, final String resourcePath)
get Resource File Relative To Base
final String baseDirUriString = baseDir != null ? baseDir.toURI().toString() : "file:/";
final boolean baseEndsWithSlash = baseDirUriString.endsWith("/");
final boolean resourcePathBeginsWithSlash = resourcePath.startsWith("/");
final String uriString;
if (baseEndsWithSlash != resourcePathBeginsWithSlash) {
    uriString = baseDirUriString + resourcePath;
} else if (baseEndsWithSlash) {
    uriString = baseDirUriString.substring(0, baseDirUriString.length() - 1) + resourcePath;
...
MapgetResourceListing(Class clazz, String path)
List directory contents for a resource folder.
URL dirURL = clazz.getClassLoader().getResource(path);
if (dirURL != null && dirURL.getProtocol().equals("file")) {
    File dirFile = new File(dirURL.toURI());
    return getSubdirFileListing(dirFile, dirURL.toExternalForm());
if (dirURL == null) {
    String me = clazz.getName().replace(".", "/") + ".class";
    dirURL = clazz.getClassLoader().getResource(me);
...
String[]getResourceListing(Class clazz, String path)
get Resource Listing
URL dirURL = clazz.getClassLoader().getResource(path);
if (dirURL != null && dirURL.getProtocol().equals("file")) {
    return new File(dirURL.toURI()).list();
if (dirURL == null) {
    String me = clazz.getName().replace(".", "/") + ".class";
    dirURL = clazz.getClassLoader().getResource(me);
if (dirURL.getProtocol().equals("jar")) {
    String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); 
    JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
    Enumeration<JarEntry> entries = jar.entries(); 
    Set<String> result = new HashSet<String>(); 
    while (entries.hasMoreElements()) {
        String name = entries.nextElement().getName();
        if (name.startsWith(path)) { 
            String entry = name.substring(path.length());
            int checkSubdir = entry.indexOf("/");
            if (checkSubdir >= 0) {
                entry = entry.substring(0, checkSubdir);
            result.add(entry);
    return result.toArray(new String[result.size()]);
throw new UnsupportedOperationException("Cannot list files for URL " + dirURL);
String[]getResourceListing(Class clazz, String path)
List directory contents for a resource folder.
URL dirURL = clazz.getClassLoader().getResource(path);
if (dirURL == null) {
    String me = clazz.getName().replace(".", "/") + ".class";
    dirURL = clazz.getClassLoader().getResource(me);
if (dirURL.getProtocol().equals("jar")) {
    String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!"));
    JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
...
ListgetResourceListing(Class clazz, String path, String glob)
List directory contents for a resource folder.
return getResourceListing(clazz, path, glob, "");
StringgetResourcePath()
Reads the relative path to the resource directory from the testResourcePath file located in src/test/resources http://stackoverflow.com/questions/21567497/how-to-output-text-to-a-file-in-resource-folder-maven
try {
    String resourcePathFile = System.class.getResource("/testResourcePath").getFile();
    String resourcePath = new BufferedReader(new FileReader(resourcePathFile)).readLine();
    URI rootURI = new File("").toURI();
    URI resourceURI = new File(resourcePath).toURI();
    URI relativeResourceURI = rootURI.relativize(resourceURI);
    return relativeResourceURI.getPath();
} catch (Exception e) {
...