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