Using java.lang.instrument with no JAR for the agent class?

R

ringofw

I am activating a Java 5 program from C++ with the invocation API and
have in memory the bytecode that make up the "agent class" i.e. the
class that contains my premain method. After creating the JVM I could
make this into a live class with ClassLoader.defineClass, but but then
it is too late to specify the VM args (and even then the
java.lang.instrument package documentation implies that the agent class
must be in a JAR file).

Is there a way to provoke the running of the premain method (to pass me
the Instrumentation instance) from the invocation API, or will I have
to write my agent class to a JAR file on the fly?

In the past I have used the undocumented sun.misc.ClassFileTransformer
so was able to ClassLoader,defineClass my ClassFileTransformer from
native code, then call a static method on it to register an instance of
itself with the class loader (i.e. doing the equivalent of what I now
want to do within premain). With the Java 5 interface I seem to have a
chicken and egg situation with this approach, at least if I persist
with having my "agent class" also be my class transformer. I can split
it into two and just have the ClassFileTransformer in in native
memory...

But if I resorted to using a JAR file to specify the agent class, and
that agent class referenced my ClassFileTransformer, is there a window
between creating the JVM and premain being activated where I can
defineClass the ClassFileTransformer that premain references? I really
don't want to have to write the ClassFileTransformer to disk too just
so it is picked up on the classpath, but can see that it is possible
that premain could be called before I explicity activate "main"(?)

Mark
 
M

Mark

I am activating a Java 5 program from C++ with the invocation API and
have in memory the bytecode that make up the "agent class" i.e. the
class that contains my premain method. After creating the JVM I could
make this into a live class with ClassLoader.defineClass, but but then
it is too late to specify the VM args (and even then the
java.lang.instrument package documentation implies that the agent class
must be in a JAR file).

Is there a way to provoke the running of the premain method (to pass me
the Instrumentation instance) from the invocation API, or will I have
to write my agent class to a JAR file on the fly?

In the past I have used the undocumented sun.misc.ClassFileTransformer
so was able to ClassLoader,defineClass my ClassFileTransformer from
native code, then call a static method on it to register an instance of
itself with the class loader (i.e. doing the equivalent of what I now
want to do within premain). With the Java 5 interface I seem to have a
chicken and egg situation with this approach, at least if I persist
with having my "agent class" also be my class transformer. I can split
it into two and just have the ClassFileTransformer in in native
memory...

But if I resorted to using a JAR file to specify the agent class, and
that agent class referenced my ClassFileTransformer, is there a window
between creating the JVM and premain being activated where I can
defineClass the ClassFileTransformer that premain references? I really
don't want to have to write the ClassFileTransformer to disk too just
so it is picked up on the classpath, but can see that it is possible
that premain could be called before I explicity activate "main"(?)

Mark

Replying to myself: one solution to my problem may be to have an
agent-class in a JAR file consisting of a premain that caches the
Instrumentation instance on a static field and then call a native
agent-class method *within* premain to both defineClass my
class-transformer and call Instrumentation.addTransformer using the
cached Instrumentation. This is probably good enough but if anyone has
a solution that doesn't involve the agent-class JAR file I'd like to
see it.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top