Java 是否可以查看类文件的字节码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3315938/
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-08-13 22:00:54  来源:igfitidea点击:

Is it possible to view bytecode of Class file?

javabytecodebytecode-manipulation

提问by Abhishek Jain

Possible Duplicate:
Is there a java classfile / bytecode editor to edit instructions?

可能的重复:
是否有用于编辑指令的 java 类文件/字节码编辑器?

Java source code is compiled into bytecode, which is actually in the class file. Is it possible to view bytecode of a compiled class?

Java源代码被编译成字节码,实际上是在class文件中。是否可以查看已编译类的字节码?

If it is possible, can it be edited?

如果可以,可以编辑吗?

Is there an eclipse plugin for that available?

是否有可用的 eclipse 插件?

回答by Andreas Dolk

The JDK comes with javapwhich is a tool to disassemble the byte code inside a class file. Editing on byte code level is possible. Have a look at BCEL, a java library designed to read, manipulate and write class files.

JDK 自带javap,它是一个反汇编类文件中的字节码的工具。可以在字节码级别进行编辑。看看BCEL,一个旨在读取、操作和写入类文件的 Java 库。

A list of tool and libraries to edit byte code can be found on java-net. For example JBE, a Java Byte Code editor that even comes with a GUI.

可以在java-net上找到用于编辑字节码的工具和库列表。例如JBE,一个甚至带有 GUI 的 Java 字节码编辑器。

回答by Jesper

Yes. You can use the javapcommand that's included with the JDK to see the byte code of a class. For example:

是的。您可以使用javapJDK 附带的命令来查看类的字节码。例如:

javap -c com.mypackage.MyClass

javap -c com.mypackage.MyClass

There are several libraries and tools that help you to work with Java bytecode, for example ASMand Jasmin.

有几个库和工具可以帮助您处理 Java 字节码,例如ASMJasmin

回答by H-H

To my experience, jclasslibis one of the best bytecode viewers.

根据我的经验,jclasslib是最好的字节码查看器之一。

As for editors, there are two types: bytecode manipulation libraries, and editors with GUIs. This question has been asked few times on SO, you could check the answers and the links that were provided.

至于编辑器,有两种类型:字节码操作库和带有 GUI 的编辑器。这个问题在 SO 上被问过几次,你可以检查答案和提供的链接。

Just be careful that editing bytecode in not as straightforward as you think. The JVMSimposes many restrictions on how class files should be, and there is a great chance that one of your edit will violate one of them.

请注意,编辑字节码并不像您想象的那么简单。该JVMS强加类文件应该如何诸多限制,而且还有很大的机会,你的编辑人会违反其中之一。

Check these other questions:

检查这些其他问题:

Editing a .class file directly, playing around with opcodes

直接编辑 .class 文件,玩弄操作码

Is it possible to view bytecode of Class file?

是否可以查看类文件的字节码?

Programming in Java bytecode

在 Java 字节码中编程

回答by Daniel Sperry

To view the bytecodes

查看字节码

Forget javap! The best plugin I have ever used is the "ASM - Bytecode Outline plugin for Eclipse"

忘记 javap!我用过的最好的插件是“ASM - Eclipse 的字节码大纲插件”

http://asm.ow2.org/eclipse/index.html

http://asm.ow2.org/eclipse/index.html

It is from ASM (a bytecode manipulation framework).

它来自 ASM(字节码操作框架)。

It shows the bytecodes (that you asked for), stack elements (jvm style), and how to generate the same result (to produce the same bytecodes) using the asm framework methods.

它显示了字节码(您要求的)、堆栈元素(jvm 样式)以及如何使用 asm 框架方法生成相同的结果(以生成相同的字节码)。

Better still is the fact that it does so while you have the source code selected. You don't have to find the .class file in the bin directory to inspect it's bytecode.

更好的是,当您选择源代码时,它会这样做。您不必在 bin 目录中找到 .class 文件来检查它的字节码。

To edit them

编辑它们

Using code:

使用代码:

  • ASM: Visitors based, very, very fast.
  • BCEL: Loads the bytecode as an in memory description of the class file.
  • Javassit: the easiest one to use, allows you to do pattern matching and expression replacement.
  • ASM:基于访客,非常非常快。
  • BCEL:加载字节码作为类文件的内存描述。
  • Javassit:最容易使用的,允许你做模式匹配和表达式替换。

By hand: JBE

手工:JBE

回答by Musznik

Try use - dirtyJOE - Java Overall Editor is a complex editor and viewer for compiled java binaries (.class files).

尝试使用 -dirtyJOE - Java 整体编辑器是用于编译的 Java 二进制文件(.class 文件)的复杂编辑器和查看器。