简体   繁体   中英

Run Java byte code

//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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM