用 Java 字节码编程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3150254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:35:00  来源:igfitidea点击:

Programming in Java bytecode

javabytecode

提问by Corey Stevens

I'm looking to write a short program (maybe a Hello World) in Java bytecode. I just want to write the bytecode using my text editor and run it. How would I do this? Got an example? Thanks!

我想用 Java 字节码编写一个简短的程序(可能是一个 Hello World)。我只想使用我的文本编辑器编写字节码并运行它。我该怎么做?有例子吗?谢谢!

采纳答案by Adam Paynter

You could try Jasmin!

你可以试试茉莉

.class public HelloWorld
.super java/lang/Object

.method public static main([Ljava/lang/String;)V
  .limit stack 3
  .limit locals 1

  getstatic      java/lang/System/out Ljava/io/PrintStream;
  ldc            "Hello World."
  invokevirtual  java/io/PrintStream/println(Ljava/lang/String;)V

  return

.end method

You compile it using:

你编译它使用:

> java -jar jasmin.jar hello.j

And then you run it like any class:

然后像运行任何类一样运行它:

> java HelloWorld
Hello World.

Update

更新

I see that your question mentions "without using Javac or Java". Could you clarify how you meant that statement?

我看到您的问题提到“不使用 Javac 或 Java”。你能澄清一下你的意思是什么吗?

回答by Antimony

I've created a new Java bytecode assemblerthat is backwards compatible with Jasmin but also adds lots of new features and simplifies the syntax slightly.

我创建了一个新的Java 字节码汇编器,它向后兼容 Jasmin,但也添加了许多新功能并稍微简化了语法。

Here's an example of how you might write a Hello World program.

下面是一个如何编写 Hello World 程序的示例。

.class public hello
.super java/lang/Object

.method public static main : ([Ljava/lang/String;)V
    .limit stack 10
    .limit locals 10

    getstatic java/lang/System out Ljava/io/PrintStream;
    ldc "Hello World!"
    invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V
    return
.end method

I've also written a tutorial on bytecode assembly. It currently only covers Hello, World, but I can continue it if there is interest.

我还写了一个关于字节码汇编的教程。目前只涵盖Hello, World,如果有兴趣我可以继续。

回答by Thorbj?rn Ravn Andersen

Byte code is written as actual bytes, which are not normally easily editable by a normal text editor.

字节码被写成实际字节,普通文本编辑器通常不容易编辑这些字节。

This means you will need something that converts a textual representation to binary. A reasonable place to start would be an assembler like Jasmin.

这意味着您将需要将文本表示转换为二进制的东西。一个合理的起点是像Jasmin这样的汇编器。

回答by bjg

Have you considered JBE(Java Bytecode Editor) ?
It's based on Apache's Bytecode Engineering Library (BCEL)

您是否考虑过JBE(Java 字节码编辑器)?
它基于 Apache 的字节码工程库 ( BCEL)

回答by bjg

Maybe this article can get you started: Bytecode basics(a little old, but you will get the idea).

也许这篇文章可以让你开始:字节码基础有点旧,但你会明白的)。

The class file formatwill come in handy too :D

类文件格式会派上用场太:d

回答by Peter Perhá?

This guy here guides you through the process of writing a class file of a hello world program step by step. Pretty exciting stuff :-) https://medium.com/@davethomas_9528/writing-hello-world-in-java-byte-code-34f75428e0ad

这里的这个家伙将引导您逐步完成编写 hello world 程序的类文件的过程。非常令人兴奋的东西:-) https://medium.com/@davethomas_9528/writing-hello-world-in-java-byte-code-34f75428e0ad