mardi 19 septembre 2017

Does 'java.lang.ClassLoader' load .class file?

For explicit loading of test.ClassLoaderTest using

public ClassLoaderTest{
      public static void main(String[] args){
        .....
        Class.forName("test.ClassLoaderTest", true,
                          ClassLoaderTest.class.getClassLoader().getParent());
        ....
      }

findClass() method of Launcher$ExtClassLoader instance gets invoked to load test.ClassLoaderTest.


On explicit loading of test.ClassLoaderTest1, using Class.forName("test.ClassLoaderTest1");, which is a wrong class file

java.lang.ClassNotFoundException: test.ClassLoaderTest1
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at test.ClassLoaderTest1.main(ClassLoaderTest.java:16)

Launcher$AppClassLoader is ultimately used to load the class.

Question:

Is java.lang.ClassLoader used only for writing custom class loader? but loading classes.





Aucun commentaire:

Enregistrer un commentaire