Class的getResource与ClassLoader的getResource路径/问题

一、Class的getResource(String path):URL
1、path 不以’/'开头时,默认是从此类所在的包下取资源;
2、path 以’/'开头时,则是从ClassPath根下获取;
System.out.println(Test.class.getResource(""));
System.out.println(Test.class.getResource("/"));
file:/E:/java/Test/bin/cn/
file:/E:/java/Test/bin/

 

二、类加载器ClassLoader的getResource(String path)

1、path 不以'/'开头

     path是指类加载器的加载范围,在资源加载的过程中,使用的逐级向上委托的形式加载的,加载范围是从整个ClassPath范围。

2、path 以'/'开头

     '/'表示Boot ClassLoader中的加载范围,因为这个类加载器是C++实现的,所以加载范围为null

System.out.println(Test.class.getClassLoader().getResource(""));
System.out.println(Test.class.getClassLoader().getResource("/"));

  file:/E:/java/Test/bin/
  null

 

三、class.getResource("/") 等价于 class.getClassLoader().getResource("")

class.getResource()与classLoader().getResource()最终使用了系统类加载器加载以classpath为根不带'/'的路径进行加载资源

Class的getResource方法源码

public URL getResource(String name){name = resolveName(name);//将路径转换成为classpath根的绝对路径,不以/开头ClassLoader cl = getClassLoader0();if (cl==null){// A system class.return ClassLoader.getSystemResource(name);}return cl.getResource(name);
}

 getClassLoader0方法与resolveName方法

//获取当前类的类加载器
ClassLoader getClassLoader0(){return classLoader; 
}//获取classpath根的绝对路径,不以/开头
private String resolveName(String name){if (name == null){return name;}//相对路径转换绝对路径if (!name.startsWith("/")){Class c = this;//获取基本类while (c.isArray()) {c = c.getComponentType();}String baseName = c.getName();  //获取类全名int index = baseName.lastIndexOf('.');if (index != -1){   //获取类包名与路径组成绝对路径name = baseName.substring(0, index).replace('.', '/')+"/"+name;}} else{//如果是以"/"开头,则去掉name = name.substring(1);}return name;
}
View Code

ClassLoader.getSystemResource方法

public static URL getSystemResource(String name) {//返回委派的系统类加载器。这是默认代表团的父母新的类加载器实例,并通常用于启动应用程序的类加载器。ClassLoader system = getSystemClassLoader();if (system == null) {return getBootstrapResource(name);//根据根加载器的路径检索资源
    }return system.getResource(name);
}
// The class loader for the system
private static ClassLoader scl;
public static ClassLoader getSystemClassLoader() {initSystemClassLoader();if (scl == null) {return null;}SecurityManager sm = System.getSecurityManager();if (sm != null) {checkClassLoaderPermission(scl, Reflection.getCallerClass());}return scl;
}
View Code

 

ClassLoader.getResource方法源码

private final ClassLoader parent;
public URL getResource(String name) {URL url;//父加载器非nullif (parent != null) {url = parent.getResource(name);  //请求父加载器获取资源} else {url = getBootstrapResource(name); //该类为ExtClassLoader,用其父加载器Bootstrap加载资源
        }if (url == null) {url = findResource(name);}return url;
}
//从虚拟机的内置类加载器查找资源
private static URL getBootstrapResource(String name) {URLClassPath ucp = getBootstrapClassPath();Resource res = ucp.getResource(name); return res != null ? res.getURL() : null;
}//返回urlclasspath是用于发现系统资源。
static URLClassPath getBootstrapClassPath() {return sun.misc.Launcher.getBootstrapClassPath();
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331584.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 轻量级文件数据库_Java:如何创建轻量级数据库微服务

java 轻量级文件数据库基于云的Java数据库应用程序的数量每分钟都在增加。 许多组织部署了数百甚至数千个微服务实例。 但是,相对于运行时环境,大多数应用程序会带来惊人的不必要开销。 反过来,这会使应用程序运行更慢,运行成本更…

html中显示shell脚本的输出,网页从shell脚本中输入并显示结果

首先,不是在BASH脚本中使用$USERNAME。 $USERNAME是一个包含当前用户名的BASH变量。实际上,在BASH中使用UPPERCASE变量通常是一个糟糕的主意。大多数BASH环境变量都是大写字母,可能会导致混淆。让你的变量小写是个好习惯。此外,因…

优先队列——二项队列(binominal queue)

【0】README 0.1) 本文文字描述部分转自 数据结构与算法分析, 旨在理解 优先队列——二项队列(binominal queue) 的基础知识; 0.2) 本文核心的剖析思路均为原创(insert,merge和del…

Class的getName、getSimpleName与getCanonicalName的区别

一、getName 除了数组外,其他的类都是输出类全名以 String 的形式返回此 Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称。 1、此类对象表示的是非数组类型的引用类型, 返回该类的二进制名称,Java…

apache.camel_Apache Camel 3.1 –即将推出更多骆驼核心优化

apache.camel希望一切都很好,您可以安全进入2020年。 Camel团队已经在忙于开发下一个Camel 3.1版本。 目标之一是继续优化骆驼核心,这一次我们花了一些时间来寻找路由引擎中的一些热点。 我们所研究的方面之一也是在Camel路由的每个消息中发生的对象分…

xp系统的计算机管理中用户在哪里,WINDOWSXP的用户管理和系统安全设置

台计算机)⑤回到“添加独立管理单元”对话框,单击“关闭”,回到““控制台->添加/删除管理单元”对话框,再单击“确定”;⑥此时,在控制台窗口左窗格中看到新添加的控制单元“本地计算机策略”;⑦依次展开…

XML——StAX Streaming API for XML(read+write)

【0】README 0.1) reshipping from http://www.journaldev.com/1191/how-to-read-xml-file-in-java-using-java-stax-api http://www.journaldev.com/892/how-to-write-xml-file-in-java-using-java-stax-api 0.2) for all source code , please visi…

Class的getInterfaces与getGenericInterface区别

一、getInterfaces 返回直接实现的接口&#xff08; 由于编译擦除&#xff0c;没有显示泛型参数&#xff09; Class<?>[] getInterfaces() 确定此对象所表示的类或接口实现的接口。 确定此对象所表示的类或接口实现的接口。 如果此对象表示一个类&am…

maven配置junit5_JUnit 5和Selenium –改善项目配置

maven配置junit5Selenium是一组支持浏览器自动化的工具和库&#xff0c;主要用于Web应用程序测试。 Selenium的组件之一是Selenium WebDriver&#xff0c;它提供客户端库&#xff0c;JSON有线协议&#xff08;与浏览器驱动程序进行通信的协议&#xff09;和浏览器驱动程序。 Se…

形容计算机网络教室的成语,形容教育的成语

形容教育的成语形容教育的成语【不教而杀】 【弦歌之声】 【化及冥顽】 【蒙以养正】【不言之教】 【沂水春风】 【嘉言善状】 【神道设教】【不教之教】 【相夫教子】 【画荻教子】 【磨昏抉聩】【东风化雨】 【因材施教】 【教无常师】 【脱骨换胎】…

Class的 getSuperclass与getGenericSuperclass区别

Class的getInterfaces与getGenericInterface区别 http://www.cnblogs.com/maokun/p/6773076.html一、getSuperclass 返回直接继承的父类&#xff08;由于编译擦除&#xff0c;没有显示泛型参数&#xff09; Class<? super T>getSuperclass() 返回表示此 Cla…

XML——XSLT的一个简单荔枝

【0】intro to XSLT&#xff08;转自&#xff1a;http://www.w3school.com.cn/xsl/xsl_languages.asp&#xff09;0.1&#xff09;起始于 XSL XSL 指扩展样式表语言&#xff08;EXtensible Stylesheet Language&#xff09;。 万维网联盟 (W3C) 开始发展 XSL 的原因是&#xff…

Eclipse系列的隐藏宝藏– 2019年版

Eclipse Collections是一个开放源代码Java Collections框架。 在此博客中&#xff0c;我将演示该框架的五个鲜为人知的功能。 我在去年的Java Advent Calendar中发布了一个类似的博客 。 请参阅博客末尾的资源以获取有关该框架的更多信息。 1. countBy() &#xff1a;当您要查…

css html 方格,使用CSS创建方格背景

这里是一个什么样的格仔背景看起来在图形设计编辑器&#xff0c;如Photoshop或Illustrator的翻版。 (所有的CSS).checkered{height: 240px;background: -webkit-linear-gradient(45deg, rgba(0, 0, 0, 0.0980392) 25%, transparent 25%, transparent 75%, rgba(0, 0, 0, 0.0980…

java嵌套类与内部类

一、嵌套类(Nested Classes) 使用嵌套类减少了命名冲突&#xff0c;一个内部类可以定义在一个类中&#xff0c;一个方法中甚至一个表达式中。 (1)定义 A nested(嵌套) class is any class whose declaration occurs within the body of another class or interface. A top lev…

jsr303 spring_使用Spring和JSR 303进行方法参数验证

jsr303 springSpring提供了一种使用JSR 303 bean验证来验证方法参数的简便方法。 在这篇文章中&#xff0c;我们将看到如何使用此功能。 建立 首先&#xff0c;我们需要通过创建MethodValidationPostProcessor bean添加对方法参数验证的支持&#xff1a; Configuration publi…

优先队列——斐波那契堆(without source code)

【0】README 0.1&#xff09; 本文部分内容转自 数据结构与算法分析&#xff0c;旨在理解 斐波那契堆 的基础知识&#xff1b; 0.2&#xff09; 文本旨在理清 斐波那契堆的 核心idea&#xff0c;还没有写出源代码实现&#xff0c;表遗憾&#xff1b; 0.3&#xff09;从实际角…

计算机专业考研可关注哪些公众号,考研应关注哪些公众号?

考研应关注哪些公众号&#xff1f;2018-11-30深夜睡不着&#xff0c;看到了这个问题&#xff0c;觉得我能帮上忙&#xff0c;就坐起来写了一份回答(^_^)微信公众号有&#xff1a;考研狗之家(特别推荐&#xff0c;各种资源)考研军火库(特别推荐&#xff0c;各种技巧)考研圈考研资…

cassandra 备份_使用sstableloader恢复Cassandra Priam备份

cassandra 备份我之前曾写过关于设置Cassandra和Priam进行备份和集群管理的文章。 但是&#xff0c;我在此处提供的用于备份还原的示例并不适用于所有情况&#xff0c;例如&#xff0c;它可能不适用于完全独立的群集。 或者在部分还原到一个表而不是整个数据库的情况下。 在这…

XML——XML概述

【0】README 0.1&#xff09;本文描述 转自 core java volume 2&#xff0c; 旨在理解 XML——XML概述 的基础知识&#xff1b; 【1】XML概述相关 1&#xff09;problemsolution &#xff08;干货——引入XML的原因&#xff09; 1.1&#xff09;problem&#xff1a;1.1.1&am…