2

I'm trying to tell the JVM to use my custom ClassLoader as default ClassLoader

This is the VM argument i use to pick my class:

-Djava.system.class.loader=JarClassLoader

and this is the error i get

Error occurred during initialization of VM
java.lang.Error: java.lang.NoSuchMethodException: JarClassLoader.<init>(java.lang.ClassLoader)
    at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
    at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
Caused by: java.lang.NoSuchMethodException: JarClassLoader.<init>(java.lang.ClassLoader)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getDeclaredConstructor(Unknown Source)
    at java.lang.SystemClassLoaderAction.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
    at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)

Do i have to define a specific method or am i using the wrong argument?

Chobeat
  • 3,445
  • 6
  • 41
  • 59
  • Is your classloader really in a default package? Does it extend from ClassLoader and override all required methods? – Perception Nov 21 '12 at 17:35
  • Actually this is a Class Loader defined by someone else to check if the optional argument i'm using works correctly. Do you have any resource where i can read which are "all the required methods"? – Chobeat Nov 21 '12 at 17:37
  • You can read through [this](http://www.javablogging.com/java-classloader-2-write-your-own-classloader/) tutorial, its pretty helpful. – Perception Nov 21 '12 at 18:26

1 Answers1

2

Custom ClassLoader

public class CustomClassLoader extends ClassLoader{

    public CustomClassLoader(ClassLoader classLoader) {
        super(classLoader);
    }

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        System.out.println("Loading class :" + name);
        return super.loadClass(name);
    }
}

Main Class

public class Main {

    public static void main(String[] args) {
        System.out.println("Starting main");
    }
}

VM arguments: -Djava.system.class.loader=CustomClassLoader

All classes are in default package and it executes successfully.

Output:
Loading class :Main
Starting main

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
  • 1
    Tried that, I am getting:Error occurred during initialization of VM java.lang.IllegalStateException: recursive invocation – Adrian Herscu Jan 15 '15 at 11:54