0

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

Pneumono
  • 17
  • 1
  • 8

2 Answers2

0

A.class has to be in the classpath somewhere for the code to run. you can put it in a jar of its own, and add the second jar to the classpath. Then the class loader can find it.

Aaron
  • 874
  • 3
  • 17
  • 34
0

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?

Community
  • 1
  • 1
Mangat Rai Modi
  • 5,397
  • 8
  • 45
  • 75