本文整理汇总了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;
}
}
示例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();
}