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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SD/MMC 卡读写模块—SD/MMC 卡的外部物理接口

    SD/MMC 卡读写模块—SD/MMC 卡的外部物理接口转载地址:http://www.8951.com/book/jiao1n21.htmSD/MMC   卡是一种大容量(最大可达4GB)、性价比高、体积小、访问接口简单的存储卡。SD/MMC卡大量应用于数码相机、MP3机、手机、大容量存储设备,作为这些便携式设备的存储载体,它还具有低功耗、非易失性、保存数据无需消耗能量等特点。SD卡接口向下兼容MMC(MutliMediaCard

    2022年6月8日
    48
  • ByteBuf用法

    ByteBuf用法JDKNIO之ByteBuffer的局限性如下:(1)长度固定,一旦分配完成,它的容量将不能动态扩展和收缩,而需要编码的POJO对象大雨ByteBuffer的容量时,会发生索引越界异常;(2)只有一个标识位置的指针position,读写的是偶需要搜公条用flip()和rewind()等,使用着必须小心的处理这些API,否则很容易导致程序越界异常;(3)ByteBuffer的API功能有限,…

    2022年9月19日
    4
  • win10中修改mac地址(总有一款适合你)

    win10中修改mac地址(总有一款适合你)我的解决办法如下:先上图:下面转自:Win10秘笈:两种方式修改网卡物理地址(MAC)https://www.ithome.com/html/win10/244510.htm在修改之前,可以先确定一下要修改的网卡MAC地址,查询方法有很多种,比如:1、在设置→网络和Internet→WLAN/以太网,如下图——2、在控制

    2022年5月12日
    80
  • mq推送数据_什么是消息队列

    mq推送数据_什么是消息队列2019独角兽企业重金招聘Python工程师标准>>>…

    2025年7月16日
    5
  • 雅典娜暴利烹饪系列(上)

    雅典娜暴利烹饪系列(上)刨冰事件田中大人曾经问过:和平是无聊的的代名词吗?答:不是。今天的圣域在纱织的领导下,依然过着比战时更加热闹的日子。  早晨出门时,修罗觉得自己是这个世界上最为幸福的人。为了庆祝他拿到特级厨师证书,女神订购了最新的微波炉和冰箱装备到他的厨房里,吹着口哨出门采购的修罗,觉得今天的阳光格外的灿烂。  修罗出门,一般总是把厨房交给卡妙照顾,卡妙是全圣域厨艺仅次于修罗的人,尤其擅长制作冷饮,其成品清凉

    2022年8月30日
    4
  • 我为什么放弃Go语言

    我为什么放弃Go语言我为什么放弃Go语言?有好几次,当我想起来的时候,总是会问自己:这个决定是正确的吗?是明智和理性的吗?其实我一直在认真思考这个问题。开门见山地说,我当初放弃Go语言,就是因为两个“不爽”:第一,对Go语言本身不爽;第二,对Go语言社区里的某些人不爽。毫无疑问,这是非常主观的结论,但是我有足够详实的客观的论据。

    2022年6月30日
    23

发表回复

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

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