Java Utililty Methods ClassLoader Load

List of utility methods to do ClassLoader Load

Description

The list of methods to do ClassLoader Load are organized into topic(s).

Method

ListgetDirectories(String path)
Locate all directories in given package
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
Enumeration<URL> resources = classLoader.getResources(path);
List<URL> dirs = new ArrayList<URL>();
while (resources.hasMoreElements()) {
    dirs.add(resources.nextElement());
return dirs;
...
FilegetFile(final String filename)
Get a File for referencing a file on the classpath.
final URL url = getURL(filename);
return new File(url.toURI());
FilegetFile(final String name)
get File
final File testFile = new File(Thread.currentThread().getContextClassLoader()
        .getResource("resources" + File.separator + name).toURI());
return testFile;
FilegetFile(Object obj, String filename)
get File
String file = obj.getClass().getPackage().getName().replace('.', '/') + '/' + filename;
URL url = obj.getClass().getClassLoader().getResource(file);
return new File(url.getFile());
StringgetFileContents(String filename)
get File Contents
StringBuffer sb = new StringBuffer();
String p = "xsp/extlib/designer/junit/resources/" + filename; 
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
int ch;
InputStream in = null;
try {
    URL url = contextClassLoader.getResource(p);
    in = url.openStream();
...
StringgetFileContents(String path)
Uses the toURL() call to convert the path and as a result can find files on the classpath.
URL url = toURL(path);
if (url == null)
    throw new IOException("File not found: " + path);
return getFileContents(new FileInputStream(url.getPath()));
FilegetFileFromPath(Object obj, String fileName)
get File From Path
ClassLoader classLoader = obj.getClass().getClassLoader();
URL resource = classLoader.getResource(fileName);
return new File(resource.getPath());
InputStreamgetFileFromPool(String path)
get File From Pool
return loader.getResourceAsStream(path);
StringgetFileFullPath(String file)
get File Full Path
String path = ResourcePath(file);
File fileTmp = new File(path);
if (fileTmp.exists()) {
    return path;
return null;
ListgetFilesInPackage(String packageName)
Given a package name, attempts to reflect to find all file names within the package on the local file system.
List<String> fns = new ArrayList<>();
String packageNameSlashed = packageName.replace(".", "/");
URL directoryURL = Thread.currentThread().getContextClassLoader().getResource(packageNameSlashed);
if (directoryURL == null) {
    System.out.println("Could not retrieve URL resource: " + packageNameSlashed);
    return fns;
String directoryString = directoryURL.getFile();
...