Load a resource from the classpath, first trying the thread context class loader, then the class loader of the given class. - Java Reflection

Java examples for Reflection:Class Loader

Description

Load a resource from the classpath, first trying the thread context class loader, then the class loader of the given class.

Demo Code


//package com.java2s;
import java.io.InputStream;

public class Main {


    /**//  ww  w.ja v a2s .  co m
     * Load a resource from the classpath, first trying the thread context
     * class loader, then the class loader of the given class.
     * @param clazz a class to try the class loader of
     * @param name the resource name
     * @return an input stream for reading the resource,
     * or null if not found
     */
    public static InputStream getResourceAsStream(Class clazz, String name) {
        InputStream in = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(name);
        if (in == null) {
            in = clazz.getResourceAsStream(name);
        }
        return in;
    }
}

Related Tutorials