当前位置: 首页>>代码示例>>Java>>正文


Java URLClassLoader.getResource方法代码示例

本文整理汇总了Java中java.net.URLClassLoader.getResource方法的典型用法代码示例。如果您正苦于以下问题:Java URLClassLoader.getResource方法的具体用法?Java URLClassLoader.getResource怎么用?Java URLClassLoader.getResource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.URLClassLoader的用法示例。


在下文中一共展示了URLClassLoader.getResource方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: tryToConnect

import java.net.URLClassLoader; //导入方法依赖的package包/类
/** Maybe connect, if not keep track of the problem.
 */
private synchronized void tryToConnect() {
    if (connected || exception != null) {
        return;
    }
    try {
        URLClassLoader l;
        String cnb = url.getHost();
        if (cnb.isEmpty()) {
            l = globalClassLoader.get();
        } else {
            l = classLoaderMap.get().get(cnb);
            if (l == null) {
                throw new IOException("no loader for " + cnb);
            }
        }
        String path = url.getPath().substring(1);
        URL u = l.getResource(path);
        if (u == null) {
            throw new FileNotFoundException(path + " in " + Arrays.toString(l.getURLs()));
        }
        real = u.openConnection();
        real.connect();
        connected = true;
    } catch (IOException ioe) {
        exception = ioe;
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:30,代码来源:CheckHelpSetsBin.java

示例2: testResources

import java.net.URLClassLoader; //导入方法依赖的package包/类
@Test(dataProvider = "resourcedata")
public void testResources(String style, URL url) throws Throwable {
    //System.out.println("  testing " + style + " url: " + url);
    URL[] urls = {url};
    URLClassLoader cldr = new URLClassLoader(urls);
    Class<?> vcls = cldr.loadClass("version.Version");

    // verify we are loading a runtime versioned class
    MethodType mt = MethodType.methodType(int.class);
    MethodHandle mh = MethodHandles.lookup().findVirtual(vcls, "getVersion", mt);
    Assert.assertEquals((int)mh.invoke(vcls.newInstance()),
            style.equals("unversioned") ? 8 : Runtime.version().major());

    // now get a resource and verify that we don't have a fragment attached
    Enumeration<URL> vclsUrlEnum = cldr.getResources("version/Version.class");
    Assert.assertTrue(vclsUrlEnum.hasMoreElements());
    URL vclsUrls[] = new URL[] {
        vcls.getResource("/version/Version.class"),
        vcls.getResource("Version.class"),
        cldr.getResource("version/Version.class"),
        vclsUrlEnum.nextElement()
    };
    Assert.assertFalse(vclsUrlEnum.hasMoreElements());
    for (URL vclsUrl : vclsUrls) {
        String fragment = vclsUrl.getRef();
        Assert.assertNull(fragment);

        // and verify that the the url is a reified pointer to the runtime entry
        String rep = vclsUrl.toString();
        //System.out.println("    getResource(\"/version/Version.class\") returned: " + rep);
        if (style.equals("http")) {
            Assert.assertTrue(rep.startsWith("jar:http:"));
        } else {
            Assert.assertTrue(rep.startsWith("jar:file:"));
        }
        String suffix;
        if (style.equals("unversioned")) {
            suffix = ".jar!/version/Version.class";
        } else {
            suffix = ".jar!/META-INF/versions/" + Runtime.version().major()
                    + "/version/Version.class";
        }
        Assert.assertTrue(rep.endsWith(suffix));
    }
    cldr.close();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:47,代码来源:MultiReleaseJarURLConnection.java


注:本文中的java.net.URLClassLoader.getResource方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。