Java agent reports "redefineClasses is not supported in this environment"
Asked Answered
C

1

6

I'm newbie on java agents. I created a simple HotswapAgent class (sniffing from 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);
        }
    } 

With this manifest:

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

And I try to reload a new class definition, in this way:

CtClass modelClass = .... 

...

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

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

All this classes are in a jar, and finally I obtain this error (on reload() call):

redefineClasses is not supported in this environment

But in Manifest is declared Can-Redefine-Classes: true.

The JVM is standard MacOS X Java 1.6 VM. This JVM works good with JRebel, that uses the same agent mechanism.

What's wrong?

Cassimere answered 11/10, 2012 at 16:23 Comment(0)
Y
2

According to the 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.

You can try to addCapability to check if there is a problem with the manifest declaration.

Here is an example of addCapability in runtime.

Yelenayelich answered 15/10, 2012 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.