集册 Java实例教程 从上下文类加载器加载资源,如果失败,则从默认类加载器加载资源。

从上下文类加载器加载资源,如果失败,则从默认类加载器加载资源。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

468
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
从上下文类加载器加载资源,如果失败,则从默认类加载器加载资源。


//package com.nowjava;

import java.net.URL;
/**
 from
* n o w    j a v a  . c o m 
**/


public class Main {

    public static void main(String[] argv) throws Exception {

        String name = "nowjava.com";

        System.out.println(loadResource(name));

    }


    /**

     * 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()
            /** 
             来自 N o w  J a v a  .   c o m**/

                    .getResource(n
展开阅读全文