是否可以查看Class文件的字节码?
可能的重复:
是否有java类文件/字节码编辑器来编辑指令?
Java源代码被编译成字节码,实际上是在class文件中。是否可以查看已编译类的字节码?
如果可以的话可以编辑一下吗?
有没有可用的 eclipse 插件?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。您可以使用 JDK 中包含的
javap
命令来查看类的字节码。例如:javap -c com.mypackage.MyClass
有几个库和工具可以帮助您使用 Java 字节码,例如 ASM 和 Jasmin。
Yes. You can use the
javap
command that's included with the JDK to see the byte code of a class. For example:javap -c com.mypackage.MyClass
There are several libraries and tools that help you to work with Java bytecode, for example ASM and Jasmin.
JDK 附带 javap 这是一个反汇编类文件中字节码的工具。可以在字节代码级别进行编辑。看一下 BCEL,一个旨在读取、操作和写入类文件的 java 库。
用于编辑字节码的工具和库的列表可以在 java-net 上找到。例如 JBE,一个甚至带有 GUI 的 Java 字节代码编辑器。
The JDK comes with javap which 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.
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.
查看字节码
忘记javap!我用过的最好的插件是 "ASM - Eclipse 的字节码大纲插件"
< a href="http://asm.ow2.org/eclipse/index.html" rel="noreferrer">http://asm.ow2.org/eclipse/index.html
它来自 ASM (字节码操作框架)。
它显示了字节码(您要求的)、堆栈元素(jvm 样式)以及如何使用 asm 框架方法生成相同的结果(生成相同的字节码)。
更好的是,当您选择源代码时,它就会执行此操作。
您不必在 bin 目录中找到 .class 文件来检查它的字节码。
编辑它们
使用代码:
手工制作:JBE
To view the bytecodes
Forget javap! The best plugin I have ever used is the "ASM - Bytecode Outline plugin for Eclipse"
http://asm.ow2.org/eclipse/index.html
It is from ASM (a bytecode manipulation framework).
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.
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.
To edit them
Using code:
By hand: JBE
根据我的经验,jclasslib 是最好的字节码查看器之一。
至于编辑器,有两种类型:字节码操作库和带有 GUI 的编辑器。这个问题已经被问过几次了,您可以检查提供的答案和链接。
请注意,编辑字节码并不像您想象的那么简单。 JVMS 对类文件的格式施加了许多限制,并且很有可能您的一项编辑将违反其中一项。
检查以下其他问题:
编辑 .class 文件直接使用操作码
是否可以查看Class文件的字节码?
Java字节码编程
To my experience, jclasslib is one of the best bytecode viewers.
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.
Just be careful that editing bytecode in not as straightforward as you think. The JVMS imposes many restrictions on how class files should be, and there is a great chance that one of your edit will violate one of them.
Check these other questions:
Editing a .class file directly, playing around with opcodes
Is it possible to view bytecode of Class file?
Programming in Java bytecode
尝试使用 - dirtyJOE - Java整体编辑器是一个复杂的编辑器和查看器,用于编译的java二进制文件(.class文件)。
Try use - dirtyJOE - Java Overall Editor is a complex editor and viewer for compiled java binaries (.class files).