Define custom system classloader

后端 未结 1 691
粉色の甜心
粉色の甜心 2021-01-15 17:55

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.l         


        
相关标签:
1条回答
  • 2021-01-15 18:38

    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

    0 讨论(0)
提交回复
热议问题