股票JDK类和“null”类加载器? [英] Stock JDK classes and the "null" ClassLoader?

查看:109
本文介绍了股票JDK类和“null”类加载器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:我试图通过查看一些动态创建的组件的 ClassLoader 来调试一个非常奇怪的类错误。 ClassLoader s是我从未玩过的东西 - 我很惊讶标准的JDK类有 null 类加载器实例。

Hi guys : Im trying to debug a very strange class error by looking at the ClassLoaders for some dynamically created components. ClassLoaders are something I've never played much with - and im surprised that standard JDK classes have null Class loader instances.

根据我试图打印的加载程序的类,有人可以解释这个简单主要方法的输出,而且更一般地说 -

Can somebody explain the output of this simple main method in terms of the classes whose loaders I am attempting to print , and also more generally -


  1. ClassLoader 在JVM上的工作方式和

  2. 我们如何使用调试缺少的类 ClassLoader s。

  1. the way ClassLoaders work on the JVM and
  2. how we can debug missing classes using ClassLoaders.



public class MyClass {

    /**
     * @param args
     */
    public static void main(String[] args) {

        System.out.println(relfect.MyClass.class.getClassLoader());
        System.out.println(String.class.getClassLoader());
        System.out.println(ArrayList.class.getClassLoader());
        System.out.println(JButton.class.getClassLoader());
        System.out.println(System.class.getClassLoader());

        Boolean b = new Boolean(true);
        System.out.println(b.getClass().getClassLoader());

    }

}

输出

sun.misc.Launcher$AppClassLoader@1f7182c1
null
null
null
null
null


推荐答案

的javadoc> getClassLoader() 表示


返回类的类加载器。某些实现可能使用null来表示引导类加载器。如果此类由引导类加载器加载,则此方法将在此类实现中返回null。

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.

所以,这至少解释了为什么你得到那个结果。但它并没有解释为什么实现者决定这样做。

So, that at least explains why you get that result. But it does not explain why the implementors decided to do it that way.

编辑:
测试后将我自己的类添加到bootclasspath然后它们也出现了作为null类加载器。

After testing adding my own classes to the bootclasspath then they also show up as null class loader.

这篇关于股票JDK类和“null”类加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆