简体   繁体   English

Javassist热交换器找不到类

[英]Javassist hot swapper not finding class

I am trying to use Javassist for the first time, and I am stuck. 我第一次尝试使用Javassist,但遇到问题。 I have a class called standard in the default package. 我在默认包中有一个叫做standard的类。 I am trying to reload it using HotSwapper. 我正在尝试使用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 我正在使用这些args启动jvm

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

The weird part is it loads the class just fine. 奇怪的是,它可以很好地加载类。

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. 您可能在谈论Javassist,但是Javassist可以读取类文件的事实并不意味着该类是由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. 这将确保Standard类已加载,因此可以重新加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM