Java Utililty Methods ClassPath Get

List of utility methods to do ClassPath Get

Description

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

Method

URLgetClasspathResource(final String resource)
get Classpath Resource
return Thread.currentThread().getContextClassLoader().getResource(resource);
URLgetClasspathResource(String name)
Finds the class path resource with the given name and returns it as a URL.
return Thread.currentThread().getContextClassLoader().getResource(name);
InputStreamgetClasspathResource(String path)
get Classpath Resource
if (path == null) {
    throw new IllegalArgumentException("the parameter 'path' is required.");
URL url = getClasspathResourceURL(path);
if (url == null) {
    throw new IllegalArgumentException("the resource path[" + path + "] could not be found.");
InputStream inStream;
...
FilegetClasspathResourceAsFile(String resourceName)
get Classpath Resource As File
return new File(
        new URI(Thread.currentThread().getContextClassLoader().getResource(resourceName).toString()));
ListgetClassPathResourcesAsStreams(String relativeResourceName)
Search resources within the classpath that match the given name, create input streams from the found resources and return them as a list.
List<InputStream> result = new ArrayList<InputStream>();
try {
    Enumeration<URL> resourceUrls = Thread.currentThread().getContextClassLoader()
            .getResources(relativeResourceName);
    while (resourceUrls.hasMoreElements()) {
        result.add(((InputStream) (resourceUrls.nextElement().getContent())));
} catch (IOException e) {
...
FilegetClassPathRoot(Class cl)
get Class Path Root
try {
    String resourceName = translateResourceName(cl);
    URL url = getClassURL(cl.getClassLoader(), resourceName);
    if (url.getProtocol().equals("file")) {
        String fileName = URLDecoder.decode(url.getPath());
        String rootDirName = fileName.replaceAll(resourceName + "$", "");
        return new File(rootDirName);
    } else {
...
StringgetClasspathString()
get Classpath String
ClassLoader cl = Optional.ofNullable(Thread.currentThread().getContextClassLoader())
        .orElse(ClassLoader.getSystemClassLoader());
while (!URLClassLoader.class.isInstance(cl)) {
    cl = cl.getParent();
    if (cl == null)
        return "Classpath is empty";
return "  " + Stream.of(cl).map(URLClassLoader.class::cast).flatMap(c -> Arrays.stream(c.getURLs()))
...