1

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.

blow
  • 12,811
  • 24
  • 75
  • 112
  • Which classes are not loaded by your CL? – biziclop Jan 23 '11 at 23:46
  • 1
    My crystal sphere gleams as if ClassNotFoundException has occurred in this particular case, whereas my java woodoo doll refuses to continue without stacktrace :) – andbi Jan 24 '11 at 00:02
  • Your crystal sphere is broken i think, i have no exception, all works, but i notice that some classes are not loaded by my class loader. (i print in stdout when load a class) – blow Jan 24 '11 at 09:39
  • 1
    side note: If you're going to decrypt at runtime then ALL of the information needed to decrypt will be available to the host/client machine. This will not make your app secure. Just harder than normal to break into. Is it worth it? Or is it a customeer requirement? – basszero Jan 26 '11 at 11:45
  • Yes is into client machine, it works as license key. With a custom decryption algorithm and hiding the key in the machine i achieve a considerable security level. And i need only to obfuscate and protect only one class, the class loader. This is defenitely more easy and strong to obfsucate all classes of my project. – blow Jan 26 '11 at 12:25
  • The JVM will load dependency classes using the class loader of the dependent class. Show the source for MyClassLoader. – Brett Kail Feb 20 '11 at 17:09

1 Answers1

0

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.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • Is a my class of my external jar. This jar is a my class libraries. – blow Jan 24 '11 at 09:40
  • @blow, I don't understand the first sentence of your question. – Mike Samuel Jan 24 '11 at 16:18
  • @blow, "Is a my class of my external jar" starts with "Is" so I assumed it was a question. – Mike Samuel Jan 25 '11 at 23:07
  • Samule: ah, sorry. No is not a question sorry. Anyway, i noticed that with my example in my first post, only Initializer are loaded through my class loader, class A is load by default class loader(i think). In some tutorial i read that sub classes( A in this case) are loaded with the same class loader of the parent( Initializer in this case), so why this does not work? – blow Jan 26 '11 at 11:35