0

I have a .class file in disc. I want to load it dynamically onto jvm using javaassist. but it throwing exception. The following is the code i wrote:

ClassPath cp=new ClassClassPath(ExampleImpl.class);
    System.out.println(cp.find(ExampleImpl.class.getName()));

        System.out.println("ExampleImpl.java");
        System.out.println(ExampleImpl.class.getName());
        System.out.println();
    CtClass ctClasz = pool.get("***D:\\ExampleImpl***");
    ctClasz.addInterface(pool.get(MyInterface.class.getName()));

There is a .class file on the D: drive and evn it is throwing the following exception:

 Exception in thread "main" javassist.NotFoundException: D:\ExampleImpl
at javassist.ClassPool.get(ClassPool.java:436)
at javaassist.Demo.main(Demo.java:24)

How to load a .class file on the disc dynamically onto jvm and execute it???

SALMAN
  • 2,031
  • 1
  • 20
  • 18
Lakshmi
  • 81
  • 2
  • 8

1 Answers1

0

Here is something without Eclipse, I think this should work (with minor modifications)

    Class<?> clazz;
    try {
        clazz = Demo.class.getClassLoader().loadClass("full.package.name.to.MyClass");
    } catch (ClassNotFoundException e) {
        System.out.println("No such class.");
        return;
    }

    MyInterface worker;
    try {
        worker = (MyInterface)clazz.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        System.out.println("Error creating actual implementation.");
        return;
    }
ioreskovic
  • 5,531
  • 5
  • 39
  • 70