In Java, is there a way (at runtime) to obtain the bytecode which defined a particular class?
Put another way, is there a way to obtain the byte[]
array passed to ClassLoader.defineClass(String name, byte[] b, int off, int len)
when a particular class was loaded? I see that this method is declared final
, so creating a custom ClassLoader
to intercept class definitions seems out of the question.
In the past, I have used the class's ClassLoader
to obtain the bytecode via the getResourceAsStream(String)
method, but I would prefer a more canonical solution.
Using javap. The Java command-line comes with the javap tool that displays information about the fields, constructors, and methods of a class file. Based on the options used, it can disassemble a class and show the instructions that comprise the Java bytecode.
Yes, a Java class file is a file containing Java bytecode that can be executed on the Java Virtual Machine (JVM).
How is Byte Code generated? Compiler converts the source code or the Java program into the Byte Code(or machine code), and secondly, the Interpreter executes the byte code on the system. The Interpreter can also be called JVM(Java Virtual Machine).
Here is a description how to implement an agent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With