java.net.URLClassLoader#getResource ( )源码实例Demo

下面列出了java.net.URLClassLoader#getResource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jkube   文件: MavenUtil.java
/**
 * Returns true if any of the given resources could be found on the given class loader
 *
 * @param project project object
 * @param paths array of strings as path
 * @return boolean value indicating whether that project has resource or not
 */
public static boolean hasResource(MavenProject project, String... paths) {
    URLClassLoader compileClassLoader = getCompileClassLoader(project);
    for (String path : paths) {
        try {
            if (compileClassLoader.getResource(path) != null) {
                return true;
            }
        } catch (Exception e) {
            // ignore
        }
    }
    return false;
}
 
源代码2 项目: swagger-brake   文件: ApiFileJarResolver.java
private URL getSwaggerFileUrl(File jarFile, JarEntry entry) {
    try {
        URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {jarFile.toURI().toURL()});
        return urlClassLoader.getResource(entry.getName());
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
}
 
源代码3 项目: netbeans   文件: CheckHelpSetsBin.java
/** 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;
    }
}
 
源代码4 项目: triplea   文件: GameChooserModel.java
private static URI getUri(final URLClassLoader loader, final String name) {
  final URL url = loader.getResource(name);
  if (url == null) {
    throw new CorruptXmlFileException(name);
  }

  return URI.create(url.toString().replace(" ", "%20"));
}
 
@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();
}