Javaagent报告“此环境不支持redefineClasses”

时间:2012-10-11 16:23:07

标签: java javaagents

我是java代理的新手。 我创建了一个简单的HotswapAgent类(从Play!Framework中嗅探):

  public class HotswapAgent {
        static Instrumentation instrumentation;
        public static boolean enabled = false;

        public static void premain(String agentArgs, Instrumentation instrumentation) 
        {
             HotswapAgent.instrumentation = instrumentation;
             HotswapAgent.enabled = true;
        }

        public static void reload(ClassDefinition... definitions) 
                             throws UnmodifiableClassException, ClassNotFoundException                         
        {
            instrumentation.redefineClasses(definitions);
        }
    } 

有了这个清单:

Manifest-Version: 1.0
Premain-Class: path.to.HotswapAgent
Can-Redefine-Classes: true

我尝试以这种方式重新加载一个新的类定义:

CtClass modelClass = .... 

...

byte [] bcode = modelClass.toBytecode();
Class c = modelClass.toClass();
modelClass.defrost();

ClassDefinition cdef = new ClassDefinition(c, bcode);
HotswapAgent.reload(cdef);

所有这些类都在一个jar中,最后我得到了这个错误(在reload()调用时):

redefineClasses is not supported in this environment

但是在Manifest中声明Can-Redefine-Classes: true

JVM是标准的MacOS X Java 1.6 VM。这个JVM适用于使用相同代理机制的JRebel。

怎么了?

1 个答案:

答案 0 :(得分:2)

根据documentation

Optional Functionality: might not be implemented for all virtual machines. The following capability (as returned by GetCapabilities) must be true to use this function.

您可以尝试addCapability检查清单声明是否存在问题。

这是example of addCapability in runtime