eclipse 如何在eclipse IDE中查看Java的字节码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3062528/
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
How to view Java's byte code in eclipse IDE?
提问by James Raitsev
Sometimes, in Eclipse , i press a combination of keys which take me to the editor page that shows contents of my .class file (bytecode). I never seem to be able to remember what that key combination is.
有时,在 Eclipse 中,我按组合键将我带到显示我的 .class 文件(字节码)内容的编辑器页面。我似乎永远无法记住那个组合键是什么。
Can someone please let me know?
有人可以让我知道吗?
Or in other words, how can one see own bytecode?
或者换句话说,如何才能看到自己的字节码?
采纳答案by Mark Peters
Eclipse's default class file viewer shows the source (see VonC's answer) if it has been associated with the binaries, otherwise it gives a javap-like view of the class (with an option to attach source). I'm guessing it's the latter that you are looking for.
Eclipse 的默认类文件查看器显示源代码(请参阅 VonC 的回答),如果它已与二进制文件关联,否则它会提供类的类似 javap 的视图(带有附加源代码的选项)。我猜你正在寻找的是后者。
I've never found a way to cleanly force Eclipse to show that output rather than the linked source. What you probably want is an Eclipse plug-in that provides Javap like functionality (or an interface to javap). Look for plugins stating they "disassemble" (as opposed to "decompile," like jad).
我从来没有找到一种干净地强制 Eclipse 显示输出而不是链接源的方法。您可能需要的是 Eclipse 插件,它提供类似 Javap 的功能(或 javap 的接口)。寻找说明它们“反汇编”(而不是“反编译”,如 jad)的插件。
Barring a plug-in, you could probably configure an external tool to perform javap but it might not play nicely with other eclipse features.
如果没有插件,您可能可以配置一个外部工具来执行 javap,但它可能无法很好地与其他 eclipse 功能配合使用。
Edit:Let me be clear about the above: If you hit F3, Eclipse does the following (generally, the actual procedure might be slightly different):
编辑:让我清楚上述内容:如果您按 F3,Eclipse 会执行以下操作(通常,实际过程可能略有不同):
- Resolves the target (if you are selecting a Java file, it will be the Java file itself; if you are selecting or your cursor is over a class name it will be the class declaration, similar for a method declaration, etc).
- Searches the build path (same project first) for a Java file containing the target. If found, opens up an writable editor displaying that Java source file.
For class/method declarations, it continues searching references on your build path for a class file that contains the declaration. If it is found, then
a) If the class file has had source attached to it, open up a read-only editor of the linked Java file.
b) If the class file does not have source attached to it, then open up a read-only panel showing the disassembled (javap-like) bytecode of the compiled class file.
- 解析目标(如果您选择的是 Java 文件,它将是 Java 文件本身;如果您正在选择或光标位于类名上,它将是类声明,类似于方法声明等)。
- 在构建路径(首先是同一项目)中搜索包含目标的 Java 文件。如果找到,则打开一个显示该 Java 源文件的可写编辑器。
对于类/方法声明,它会继续在构建路径上搜索包含该声明的类文件的引用。如果找到了,那么
a) 如果类文件附加了源代码,请打开链接的 Java 文件的只读编辑器。
b) 如果类文件没有附加源代码,则打开一个只读面板,显示已编译类文件的反汇编(类 javap)字节码。
My guess would be that you're thinking there's a dedicated key sequence to 3.b), but I don't think there is. But again, I would love to be proven wrong here.
我的猜测是您认为 3.b) 有一个专用的键序列,但我认为没有。但同样,我很想在这里被证明是错误的。
回答by alex2k8
You can use ASM 4.0 Plugin.
您可以使用 ASM 4.0 插件。
Installation
安装
The Bytecode Outline plugin can be installed from the Eclipse Update Manager with the ObjectWeb Eclipse Update Site http://download.forge.objectweb.org/eclipse-update/Alternatively, the plugin can be downloaded from the ObjectWeb Forge site, and manually installed in the Eclipse plugins directory.
Bytecode Outline 插件可以通过 ObjectWeb Eclipse 更新站点http://download.forge.objectweb.org/eclipse-update/从 Eclipse 更新管理器安装 或者,可以从 ObjectWeb Forge 站点下载插件,然后手动安装在 Eclipse 插件目录中。
Usage
用法
To see the bytecode of the current .java or .class file:
查看当前 .java 或 .class 文件的字节码:
Window -> Show View -> Other -> Java -> Bytecode
窗口 -> 显示视图 -> 其他 -> Java -> 字节码
To compare the bytecode of two .java or .class files:
比较两个 .java 或 .class 文件的字节码:
select two *.class or *.java files -> right click -> Compare with -> Each Other Bytecode
选择两个 *.class 或 *.java 文件 -> 右键单击 -> 比较 -> 彼此的字节码
or
或者
select one *.class or *.java file -> right click -> Compare with -> Another Class Bytecode
选择一个 *.class 或 *.java 文件 -> 右键单击 -> 比较 -> 另一个类字节码
回答by Premraj
Using the following external tool configuration we can view java byte code in eclipse with the help of javap
:
使用以下外部工具配置,我们可以借助以下工具在eclipse中查看java字节码javap
:
To get the above pop-up select Run
option and select External Tools
and select External Tools configuration...
获取上面弹出的选择Run
选项并选择External Tools
并选择External Tools configuration...
${system_path:javap}
is used to locate javap in the JDK used by the Eclipse. You can use an absolute path to javap instead.
${system_path:javap}
用于在Eclipse 使用的JDK 中定位javap。您可以改用 javap 的绝对路径。
${project_loc}
returns the absolute path to the project. This is used, since I could not find a pre-define variable to use, to locate the .class
file of a resource, and that's why javap
runs in the project's directory instead of the directory containing the .class
file.
${project_loc}
返回项目的绝对路径。这是使用的,因为我找不到要使用的预定义变量来定位.class
资源的文件,这就是为什么javap
在项目目录而不是包含.class
文件的目录中运行的原因。
Among the arguments passed to javap
:
在传递给 的参数中javap
:
bin
is the default output folder for Eclipse projects. Change this tobuild/classes
or whatever is used by the project. Note, the value is relative to${project_loc}
; you can specify absolute paths instead.${java_type_name}
is used to obtain the selected class name.
bin
是 Eclipse 项目的默认输出文件夹。将此更改为build/classes
或项目使用的任何内容。请注意,该值是相对于${project_loc}
; 您可以改为指定绝对路径。${java_type_name}
用于获取选定的类名。
You can select a Java file in the Project explorer view or Project navigator view, or even a Java type in any of the views, and then run the external tool. Note - this approach doesn't work quite well when you select a method, an inner class etc. and then run the tool, as they are not resources on their own, leading to the scenario where ${project_loc}
will be empty.
您可以在项目资源管理器视图或项目导航器视图中选择 Java 文件,甚至可以在任何视图中选择 Java 类型,然后运行外部工具。注意 - 当您选择一个方法、一个内部类等然后运行该工具时,这种方法不能很好地工作,因为它们本身不是资源,导致场景${project_loc}
为空。
Dr. Garbage Tools is a suite of Eclipse Plugins released under Apache Open Source license.
Dr. Garbage Tools 是一套在 Apache 开源许可下发布的 Eclipse 插件。
Install Bytecode Visualizerusing Eclipse Marketplace.
使用 Eclipse Marketplace安装Bytecode Visualizer。
To view a bytecode:
查看字节码:
Right click on
.java
file, selectopen with
and selectother
you will get the following popup:Search "Bytecode Visualizer" and select the option it opens the file as follows there you can find Bytecode option as shown:
"Window > Preferences > General > Editors > File Associations"
“窗口 > 首选项 > 常规 > 编辑器 > 文件关联”
Change default to your for both .class association.
将两个 .class 关联的默认值更改为您的。
"*.class" : "Class Decompiler Viewer" is selected by default.
“ *.class” :默认情况下选择“类反编译器查看器”。
"*.class without source" : "Class Decompiler Viewer" is selected by default.
“ *.class without source”:默认选择“Class Decompiler Viewer”。
回答by VonC
Well... if the .class
is selected in the Navigator View or Package Explorer view, a simple F3is enough to open a decompiled version of it in the editor.
嗯...如果.class
在导航器视图或包资源管理器视图中选择了,一个简单的F3就足以在编辑器中打开它的反编译版本。
回答by JBert
As hinted at by user833970: The Bytecode Outline plugin: http://andrei.gmxhome.de/bytecode/index.html
正如 user833970 所暗示的:字节码大纲插件:http://andrei.gmxhome.de/bytecode/index.html
At its base, it provides a "bytecode" view for the currently opened Java file which is what you were looking for.
在它的基础上,它为当前打开的 Java 文件提供了一个“字节码”视图,这正是您正在寻找的。
However, you can also assign it to load any random .class file instead of the default .class viewer, and it surpasses it in many ways. For example, this viewer actually jumps to the right spot in the bytecode if you click on a member in the Outline view.
但是,您也可以指定它加载任何随机 .class 文件而不是默认的 .class 查看器,并且它在很多方面都超过了它。例如,如果您单击 Outline 视图中的成员,该查看器实际上会跳转到字节码中的正确位置。
Download links (for dropin use) can be found at the afore-mentioned link, the update-site is http://andrei.gmxhome.de/eclipse/.
下载链接(用于 dropin 使用)可以在上述链接中找到,更新站点是http://andrei.gmxhome.de/eclipse/。
回答by HesNotTheStig
I've tried several different solutions for this. Bytecode Visualizer (available here: https://sourceforge.net/projects/drgarbagetools/) is, in my opinion, the best solution. It neatly displays the output and makes clear how it links to the source code. I've noticed a few minor misinterpretations on some of the bytecodes, but you can get the gist of it.
为此,我尝试了几种不同的解决方案。Bytecode Visualizer(可在此处获得:https: //sourceforge.net/projects/drgarbagetools/)在我看来是最好的解决方案。它整齐地显示输出并清楚地说明它如何链接到源代码。我注意到对某些字节码有一些小的误解,但您可以了解其要点。
Unfortunately, it doesn't seem to support Neon at time of writing. It does support Mars, though.
不幸的是,在撰写本文时,它似乎不支持 Neon。不过,它确实支持火星。
回答by Arne Deutsch
If you are really interested in the bytecode there is a eclipse plugin that lets you open and edit the bytecode of a .class file using a text editor.
如果您真的对字节码感兴趣,可以使用 eclipse 插件,让您使用文本编辑器打开和编辑 .class 文件的字节码。