JVM: Inspect class files with `javap`
javap
can be used to inspect the generated bytecode in a java .class
file.
For example, given a kotlin file, foo.kt
:
// foo.kt
fun main() {
println("Hello, world!")
}
Compile it to a class file:
kotlinc -d out foo.kt
tree out
# out
# ├── FooKt.class
# └── META-INF
# └── main.kotlin_module
Then inspect the generated bytecode:
javap -c out/FooKt.class
# Compiled from "foo.kt"
# public final class FooKt {
# public static final void main();
# Code:
# 0: ldc #8 // String Hello, world!
# 2: getstatic #14 // Field java/lang/System.out:Ljava/io/PrintStream;
# 5: swap
# 6: invokevirtual #20 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
# 9: return
#
# public static void main(java.lang.String[]);
# Code:
# 0: invokestatic #23 // Method main:()V
# 3: return
# }
Published on: 02 Dec 2024