简体   繁体   中英

java custom classloader: some classes are not loaded by my classloader

i'm writing a custom classloader, i'have set it to be the default classloader by using the parameter

-Djava.system.class.loader=MyClassLoader

Most of the classes are loaded by my classloader, but some classes not, why? This classes are into an external jar file.

UPDATE Here an example

public class Main{
    public static void main(String[] args) {
        try{
            // A simple class loader, ovveride loadClass
            // method and print in stdout the name of the class loaded.
            MyClassLoader classLoader=new MyClassLoader(MyClassLoader.class.getClassLoader());
            Class init=classLoader.loadClass("Initializer");
            Object instance=init.newInstance();
            init.getMethod("init").invoke(instance);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

public class A{

    public A() {
        System.out.println("Im A");
    }
}

public class Initializer {

     public void init() {
        A a=new A();
    }
}

The problem is: I expect that class A are loaded by my class loader, but this is does not happen, why?

UPDATE

Anyway, i want to load ALL my classes with my class loader, becouse i want to encrypt class code and decrypt it at runtime. So, how can i use my class loader as default class loader for ALL my classes?

Thanks.

Anything under java.lang will always be loaded by the bootstrap classloader.

From http://en.wikipedia.org/wiki/Java_Classloader :

When the JVM is started, three class loaders are used[3][4]:

  1. Bootstrap class loader
  2. Extensions class loader
  3. System class loader

The bootstrap class loader loads the core Java libraries[5] (/lib directory). This class loader, which is part of the core JVM, is written in native code.

The extensions class loader loads the code in the extensions directories (/lib/ext or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM