Question

I am trying to use Javassist for the first time, and I am stuck. I have a class called standard in the default package. I am trying to reload it using HotSwapper. I have tried this but it doesnt work.

public static void main(String[] args) throws Exception
{
    ClassPool pool = ClassPool.getDefault();
    CtClass clazz = pool.get("Standard");
    HotSwapper swap = new HotSwapper(8000);
    swap.reload("Standard", clazz.toBytecode());
}

This is the error I get

Exception in thread "main" java.lang.RuntimeException: no such class: Standard
at javassist.util.HotSwapper.toRefType(HotSwapper.java:189)
at javassist.util.HotSwapper.reload(HotSwapper.java:157)
at JavaHacks.main(JavaHacks.java:15)

I am launching jvm with these args

agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000

The weird part is it loads the class just fine.

Was it helpful?

Solution

The weird part is it loads the class just fine.

You are probably talking about Javassist, but the fact that Javassist can read the class file does not mean that the class was classloaded by the JVM.

You should change your code to :

public static void main(String[] args) throws Exception {
    // Ensure Standard class classloading by creating an instance 
    // (calling a static method like Standard.init() would also do)
    Standard standard = new Standard();
    standard.doSomething();
    ClassPool pool = ClassPool.getDefault();
    CtClass clazz = pool.get("Standard");
    HotSwapper swap = new HotSwapper(8000);
    swap.reload("Standard", clazz.toBytecode());
}

This will ensure that the Standard class was classloaded, hence can be reloaded.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top