Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

java - Stock JDK classes and the "null" ClassLoader?

I am 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. 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());
        
    }

}

Output

sun.misc.Launcher$AppClassLoader@1f7182c1
null
null
null
null
null
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The javadoc for getClassLoader() says

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...