Java Code Examples for java.net.URLClassLoader#getResource()

The following examples show how to use java.net.URLClassLoader#getResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MavenUtil.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: ApiFileJarResolver.java    From swagger-brake with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 3
Source File: CheckHelpSetsBin.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** 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;
    }
}
 
Example 4
Source File: GameChooserModel.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
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"));
}
 
Example 5
Source File: MultiReleaseJarURLConnection.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@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();
}