How to view Java's byte code in eclipse IDE?

2019-01-17 07:20发布

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.

Can someone please let me know?

Or in other words, how can one see own bytecode?

7条回答
啃猪蹄的小仙女
2楼-- · 2019-01-17 07:49

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.

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).

Barring a plug-in, you could probably configure an external tool to perform javap but it might not play nicely with other eclipse features.

Edit: Let me be clear about the above: If you hit F3, Eclipse does the following (generally, the actual procedure might be slightly different):

  1. 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).
  2. 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.
  3. 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.

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.

查看更多
Root(大扎)
3楼-- · 2019-01-17 07:51

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.

https://github.com/itemis/jbc

enter image description here

查看更多
叼着烟拽天下
4楼-- · 2019-01-17 07:59

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.

Unfortunately, it doesn't seem to support Neon at time of writing. It does support Mars, though.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-17 08:08

You can use ASM 4.0 Plugin.

enter image description here

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.

Usage

To see the bytecode of the current .java or .class file:

Window -> Show View -> Other -> Java -> Bytecode

To compare the bytecode of two .java or .class files:

select two *.class or *.java files -> right click -> Compare with -> Each Other Bytecode

or

select one *.class or *.java file -> right click -> Compare with -> Another Class Bytecode

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

查看更多
Explosion°爆炸
6楼-- · 2019-01-17 08:10

Well... if the .class is selected in the Navigator View or Package Explorer view, a simple F3 is enough to open a decompiled version of it in the editor.

http://img822.i_mageshack.us/img822/8735/eclipseclass.png

查看更多
趁早两清
7楼-- · 2019-01-17 08:14

As hinted at by user833970: The Bytecode Outline plugin: 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.

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.

Download links (for dropin use) can be found at the afore-mentioned link, the update-site is http://andrei.gmxhome.de/eclipse/.

查看更多
登录 后发表回答