理解 Java 字节码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1552308/
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
Understanding Java Byte Code
提问by hhafez
Often I am stuck with a java class file with no source and I am trying to understand the problem I have at hand.
我经常被一个没有源代码的 java 类文件困住,我试图理解我手头的问题。
Note a decompiler is useful but not sufficient in all situation...
请注意,反编译器很有用,但在所有情况下都不够用...
I have two question
我有两个问题
- What tools are available to view java byte code (preferably available from the linux command line )
- What are good references to get familiar with java byte code syntax
- 有哪些工具可以查看java字节码(最好从linux命令行获取)
- 熟悉java字节码语法有哪些好的参考资料
回答by coobird
Rather than looking directly at the Java bytecode, which will require familiarity with the Java virtual machine and its operations, one could try to use a Java decompiling utility. A decompiler will attempt to create a javasource file from the specified classfile.
与其直接查看需要熟悉 Java 虚拟机及其操作的 Java 字节码,不如尝试使用 Java 反编译实用程序。反编译器将尝试java从指定class文件创建源文件。
The How do I “decompile” Java class files?is a related question which would be informative for finding out how to decompile Java classfiles.
该怎么做我“编译” Java类文件?是一个相关的问题,它可以为找出如何反编译 Javaclass文件提供信息。
That said, one could use the javapcommand which is part of the JDK in order to disassemble Java classfiles. The output of javapwill be the Java bytecode contained in the classfiles. But do be warned that the bytecode does not resemble the Java source code at all.
也就是说,可以使用作为javapJDK 一部分的命令来反汇编 Javaclass文件。的输出javap将是class文件中包含的 Java 字节码。但请注意,字节码根本不像 Java 源代码。
The definite source for learning about the Java bytecode and the Java Virtual Machine itself would be The Java Virtual Machine Specification, Second Edition. In particular, Chapter 6: The Java Virtual Machine Instruction Sethas an index of all the bytecode instructions.
了解 Java 字节码和 Java 虚拟机本身的明确来源是The Java Virtual Machine Specification, Second Edition。特别是,第 6 章:Java 虚拟机指令集包含所有字节码指令的索引。
回答by Randy Sugianto 'Yuku'
To view bytecode instruction of class files, use the javap -vcommand, the same way as if you run a java program, specifying classpath (if necessary) and the class name.
查看类文件的字节码指令,使用该javap -v命令,就像运行java程序一样,指定类路径(如果需要)和类名。
Example:
例子:
javap -v com.company.package.MainClass
About the bytecode instruction set, Instruction Set Summary
关于字节码指令集, Instruction Set Summary
回答by Denis Tulskiy
Fernfloweris an analytical decompiler, so it will decompile classes to a readable java code instead of bytecodes. It's much more usefull when you want to understand how code works.
Fernflower是一个解析反编译器,因此它将类反编译为可读的 java 代码而不是字节码。当您想了解代码的工作原理时,它会更有用。
回答by James Black
If you have a class and no source code, but you have a bug, you can do one of two basic things:
如果您有一个类并且没有源代码,但是您有一个错误,您可以执行以下两个基本操作之一:
- Decompile, fix the bug and recreate the jar file. I have done this before, but sysadmins are leery about putting that into production.
Write unit tests for the class, determine what causes the bug, report the bug with the unit tests and wait for it to be fixed.
(2) is generally the one that sysadmins, in my experience, prefer.
If you go with (2) then, in the meantime, since you know what causes the bug, you can either not allow that input to go to the class, to prevent a problem, or be prepared to properly handle it when the error happens.
You can also use AspectJ to inject code into the problem class and change the behavior of the method without actually recompiling. I think this may be the preferable option, as you can change it for all code that may call the function, without worrying about teaching everyone about the problem.
If you learn to read the bytecode instructions, what will you do to solve the problem?
- 反编译,修复错误并重新创建jar文件。我以前这样做过,但系统管理员对将其投入生产持怀疑态度。
为类编写单元测试,确定导致错误的原因,用单元测试报告错误并等待修复。
(2) 通常是系统管理员,根据我的经验,更喜欢的。
如果你使用 (2) 那么,与此同时,因为你知道是什么导致了错误,你可以不允许该输入进入类,以防止出现问题,或者准备在错误发生时正确处理它.
您还可以使用 AspectJ 将代码注入问题类并更改方法的行为,而无需实际重新编译。我认为这可能是更可取的选择,因为您可以为所有可能调用该函数的代码更改它,而不必担心将问题教给每个人。
如果你学会阅读字节码指令,你会怎么做来解决这个问题?
回答by Stephen C
I have two question
1) What tools are available to view java byte code (preferably available from the linux command line )
我有两个问题
1)有哪些工具可以查看java字节码(最好从linux命令行获取)
The javaptool (with the -c option) will disassemble a bytecode file. It runs from the command line, and is supplied as part of the Java SDK.
该javap工具(带有 -c 选项)将反汇编一个字节码文件。它从命令行运行,并作为 Java SDK 的一部分提供。
2) What are good references to get familiar with java byte code syntax
2)熟悉java字节码语法有哪些好的参考资料
The javaptool uses the same syntax as is used in the JVM specification, and the JVM spec is naturally the definitive source. I also spotted "Inside the Java Virtual Machine" by Bill Venners. I've never read it, and it looks like it might be out of print.
该javap工具使用与 JVM 规范相同的语法,JVM 规范自然是权威来源。我还发现了 Bill Venners 的“Inside the Java Virtual Machine”。我从来没有读过它,它看起来可能已经绝版了。
The actual (textual) syntax is simple and self explanatory ... assuming that you have a reference that explains what the bytecodes do, and that you are moderately familiar with reading code at this level. But it is likely to be easier to read the output of a decompiler, even if the bytecodes has been fed through an obfuscator.
实际的(文本)语法很简单且不言自明……假设您有一个解释字节码功能的参考资料,并且您对阅读此级别的代码有一定程度的熟悉。但读取反编译器的输出可能更容易,即使字节码是通过混淆器提供的。
回答by Thorbj?rn Ravn Andersen
You might find the Eclipse Byte Code Outline plugin useful:
您可能会发现 Eclipse Byte Code Outline 插件很有用:
http://andrei.gmxhome.de/bytecode/index.html
http://andrei.gmxhome.de/bytecode/index.html
I have not used it myself - just seen it mentioned in passing.
我自己没有使用它 - 只是看到它顺便提到。

