java getclassloader_java-关于getClass().getClassLoader()

java getclassloader_java-关于getClass().getClassLoader()InputStreamis=getClass().getClassLoader().getResourceAsStream(“helloworld.properties”);中getClass()和getClassLoader()都是什么意思呀.getClass():取得当前对象所属的Class对象getClassLoader():取得该Class对象的类装载器类装载器负责从Java字符文件…

大家好,又见面了,我是你们的朋友全栈君。

InputStream is = getClass().getClassLoader().getResourceAsStream(“helloworld.properties”);中getClass()和getClassLoader()都是什么意思呀.

getClass():取得当前对象所属的Class对象

getClassLoader():取得该Class对象的类装载器

类装载器负责从Java字符文件将字符流读入内存,并构造Class类对象,在你说的问题哪里,通过它可以得到一个文件的输入流

getClass :

public final Class getClass()

Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.

Returns:

the object of type Class that represents the runtime class of the object.

getClassLoader

public ClassLoader getClassLoader()

Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.

If a security manager is present, and the caller´s class loader is not null and the caller´s class loader is not the same as or an ancestor of the class loader for the class whose class loader is requested, then this method calls the security manager´s checkPermission method with a RuntimePermission(“getClassLoader”) permission to ensure it´s ok to access the class loader for the class.

If this object represents a primitive type or void, null is returned.

Returns:

the class loader that loaded the class or interface represented by this object.

Throws:

SecurityException – if a security manager exists and its checkPermission method denies access to the class loader for the class.

See Also:

ClassLoader, SecurityManager.checkPermission(java.security.Permission), RuntimePermission

Class.getClassLoader()的一个小陷阱:)

昨天我的code总在Integer.class.getClassLoader().getResource(“*********”);这一句抛出空指针异常,定位为getClassLoader()返回null,查了一下jdk的文档,原来这里还有一个陷阱:

jdk中关于getClassLoader()的描述:

/**

* Returns the class loader for the class. Some implementations may use

* null to represent the bootstrap class loader. This method will return

* null in such implementations if this class was loaded by the bootstrap

* class loader.

*

*

If a security manager is present, and the caller’s class loader is

* not null and the caller’s class loader is not the same as or an ancestor of

* the class loader for the class whose class loader is requested, then

* this method calls the security manager’s checkPermission

* method with a RuntimePermission("getClassLoader")

* permission to ensure it’s ok to access the class loader for the class.

*

*

If this object

* represents a primitive type or void, null is returned.

.....

上面的英文可以用下面的话来理解:

装载类的过程非常简单:查找类所在位置,并将找到的Java类的字节码装入内存,生成对应的Class对象。Java的类装载器专门用来实现这样的过程,JVM并不止有一个类装载器,事实上,如果你愿意的话,你可以让JVM拥有无数个类装载器,当然这除了测试JVM外,我想不出还有其他的用途。你应该已经发现到了这样一个问题,类装载器自身也是一个类,它也需要被装载到内存中来,那么这些类装载器由谁来装载呢,总得有个根吧?没错,确实存在这样的根,它就是神龙见首不见尾的Bootstrap ClassLoader. 为什么说它神龙见首不见尾呢,因为你根本无法在Java代码中抓住哪怕是它的一点点的尾巴,尽管你能时时刻刻体会到它的存在,因为java的运行环境所需要的所有类库,都由它来装载,而它本身是C++写的程序,可以独立运行,可以说是JVM的运行起点,伟大吧。在Bootstrap完成它的任务后,会生成一个AppClassLoader(实际上之前系统还会使用扩展类装载器ExtClassLoader,它用于装载Java运行环境扩展包中的类),这个类装载器才是我们经常使用的,可以调用ClassLoader.getSystemClassLoader() 来获得,我们假定程序中没有使用类装载器相关操作设定或者自定义新的类装载器,那么我们编写的所有java类通通会由它来装载,值得尊敬吧。AppClassLoader查找类的区域就是耳熟能详的Classpath,也是初学者必须跨过的门槛,有没有灵光一闪的感觉,我们按照它的类查找范围给它取名为类路径类装载器。还是先前假定的情况,当Java中出现新的类,AppClassLoader首先在类传递给它的父类类装载器,也就是Extion ClassLoader,询问它是否能够装载该类,如果能,那AppClassLoader就不干这活了,同样Extion ClassLoader在装载时,也会先问问它的父类装载器。我们可以看出类装载器实际上是一个树状的结构图,每个类装载器有自己的父亲,类装载器在装载类时,总是先让自己的父类装载器装载(多么尊敬长辈),如果父类装载器无法装载该类时,自己就会动手装载,如果它也装载不了,那么对不起,它会大喊一声:Exception,class not found。有必要提一句,当由直接使用类路径装载器装载类失败抛出的是NoClassDefFoundException异常。如果使用自定义的类装载器loadClass方法或者ClassLoader的findSystemClass方法装载类,如果你不去刻意改变,那么抛出的是ClassNotFoundException。

这里jdk告诉我们:如果一个类是通过bootstrap 载入的,那我们通过这个类去获得classloader的话,有些jdk的实现是会返回一个null的,比如说我用 new Object().getClass().getClassLoader()的话,会返回一个null,这样的话上面的代码就会出现NullPointer异常.所以保险起见我们最好还是使用我们自己写的类来获取classloader(“this.getClass().getClassLoader()“),这样一来就不会有问题。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134187.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • gif动图录制工具_手机录制gif软件

    gif动图录制工具_手机录制gif软件现在好多gif图片合成是收费的,而且可能还不太好用,这里分析的gif合成软件是个比较老的软件,但是用着还是挺好用的。还有一个录屏软件,录制保存为gif文件。![软件工具](https://img-blog.csdnimg.cn/c45166c2911b4600b6a33244da0b8e26.png)百度网盘分享,无需积分:链接:https://pan.baidu.com/s/1HukTW6yJvqoUiqbzXuY5bQ提取码:pvc4欢迎关注微信公众号,分享更多实用工具:…

    2025年11月29日
    6
  • 像素和毫米的换算_1500像素等于多少毫米

    像素和毫米的换算_1500像素等于多少毫米屏幕PPI计算:(White^2+Height^2)^0.5/屏幕大小英寸数毫米和像素换算:mm=(px/dpi)*25.4px=(mm*dpi)/25.4英寸=px/dpi1英寸=25.4毫

    2022年8月2日
    22
  • python面试题及答案_微型计算机原理与接口第四版答案

    python面试题及答案_微型计算机原理与接口第四版答案题目目录什么是接口测试?为什么要做接口测试/接口测试有哪些优势?请问你们公司是如何做接口测试的?怎么设计接口测试用例?没有接口文档如何做接口测试?怎么区分bug是前端还是后端的bug?常用的接口测试工具?接口之间数据依赖如何处理?依赖于第三方数据的接口如何进行测试?你平常做接口测试的过程中发现过哪些bug?当接口出现异常时候,你是如何分析异常的?什么是接口测试?接口测试就是通过测试不同情况下的入参与之相应的出参信息来判断接口是否符合或满足相应的功能性、安全性要求测试的重点是

    2022年9月26日
    3
  • 2021年河北高考成绩位次怎么查询,2021年河北高考一分一段表查询排名方法 成绩排名位次什么时候公布…

    2021年河北高考成绩位次怎么查询,2021年河北高考一分一段表查询排名方法 成绩排名位次什么时候公布…河北高考填报志愿最关键的第一步,就是在河北高考一分一段表查询排名方法考生要在“一分一段表”中查找与自己成绩相对应的分数排名,以及这一位次的成绩共有多少考生,以此来确定个人成绩在全省的位置。同时查一下自己的目标院校在往年的录取线是多少,该院校的录取线在当年“一份一段表”中的位置再查一下该院校在当年的招生人数,参照今年的招生计划人数,综合判断自己今年的位置是否能够被目标院校录取。河北高考成绩排名位次一…

    2022年7月16日
    50
  • Linux下搭设游戏服务器实例大全

    Linux下搭设游戏服务器实例大全半条命hlds_l_3110_full.bin点击下载http://www.mudtx.com/download/hlds_l_3110_full.bincs1.5cs_15_full.tar.gz点击下载http://www.mudtx.com/download/cs_15_full.tar.gz免cdkey补丁engine_i386.so点击下载http://down.cs-cn….

    2022年7月14日
    13
  • LinkedHashMap和hashMap和TreeMap的区别「建议收藏」

    LinkedHashMap和hashMap和TreeMap的区别「建议收藏」区别:LinkedHashMap是继承于HashMap,是基于HashMap和双向链表来实现的。 HashMap无序;LinkedHashMap有序,可分为插入顺序和访问顺序两种。如果是访问顺序,那put和get操作已存在的Entry时,都会把Entry移动到双向链表的表尾(其实是先删除再插入)。 LinkedHashMap存取数据,还是跟HashMap一样使用的Entry[]的方式,双向…

    2025年6月24日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号