Java 代理报告“此环境不支持 redefineClasses”

问题描述 投票:0回答:1

我是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 配合良好,使用相同的代理机制。

怎么了?

java javaagents
1个回答
2
投票

根据文档

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
检查manifest声明是否有问题。

这是一个 运行时 addCapability 的示例

© www.soinside.com 2019 - 2024. All rights reserved.