Loads a resource from the context class loader or, if that fails, from the default class loader. - Java Reflection

Java examples for Reflection:Class Loader

Description

Loads a resource from the context class loader or, if that fails, from the default class loader.

Demo Code


//package com.java2s;
import java.net.URL;

public class Main {
    public static void main(String[] argv) throws Exception {
        String name = "java2s.com";
        System.out.println(loadResource(name));
    }/* w w w  .j  av a  2  s .  c  om*/

    /**
     * Loads a resource from the context class loader or, if that fails, from
     * the default class loader.
     * 
     * @param name
     *            is the name of the resource to load.
     * @return a <code>URL</code> object.
     */
    public static URL loadResource(final String name) {
        try {
            return Thread.currentThread().getContextClassLoader()
                    .getResource(name);
        } catch (Exception e) {
            return ClassLoader.class.getClassLoader().getResource(name);
        }
    }
}

Related Tutorials