Search code examples
javaclassloader

Change the system class loader at runtime


Main class

public class Main {
    public static void main(String[] args) {
        // Class load
        A a = new A();
        a.msg();
    }
}

A class

public class A {
    public void msg() {
      System.out.println("msg");
    }
}

I have written code in the main class that calls a msg() method of class A

After I created the jar file, I pull out A.class.

Then the path will have a jar file with missing A.class, and A.class.

A a = new A();
a.msg();

How do I dynamically load and run A.class without making any changes to the above code?

Please help me..


Solution

  • Looks like you want to load a class dynamically. I would recommend you to create a jar and load it using URIClassLoader. There is a really good answer here:- How should I load Jars dynamically at runtime?