Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Java byte code

Tags:

java

bytecode

//bytecode of <init>
0:    aload_0
1:    invokespecial java.lang.Object.<init> ()V (8)
4:    return
//bytecode of <main>
0:    getstatic     java.lang.System.out Ljava/io/PrintStream; (16)
3:    ldc       "Hello World" (22)
5:    invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V (24)
8:    return

The above is the byte code for :

public class Hi {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

If I have only the byte code, how do I run it? Can I? And what's the extention of the byte code file? Hi.class?

like image 229
Daanish Avatar asked May 28 '26 01:05

Daanish


1 Answers

You can compile byte code using Jasmin which has its own format. You need the byte code for the entire class, not just the body of a method.

Once you have the byte code you can load it in a custom class loader or call defineClass on the current class loader and run it.

You may find that compiling and running from the source is easier with the use of the Compiler API or BeanShell.

like image 76
Peter Lawrey Avatar answered May 30 '26 14:05

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!