23

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?

prayagupa
  • 30,204
  • 14
  • 155
  • 192
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • 1
    you should change the question's title. It is IDE centric, otherwise it duplicates this one http://stackoverflow.com/q/14146782/269514. – Gilberto Mar 20 '15 at 23:48

7 Answers7

11

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.

Tyler
  • 21,762
  • 11
  • 61
  • 90
Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • There is some default key combination that shows bytecode. Excuse me if i was not clear. That is what i was referring to – James Raitsev Jun 17 '10 at 17:35
  • You were clear, and I understood fine. I just believe you're mistaken, or that maybe that functionality is provided by a non-stock plugin. I would love to be proven wrong since I could use such a shortcut. If you know this to be true, why don't you just go through Preferences->General->Keys, find the binding, and report back here? Thanks. – Mark Peters Jun 17 '10 at 18:12
  • And by the way, my answer *does* deal with bytecode. It's what I mean when I say a javap-like view. Javap is the JDK tool for displaying bytecode from a compiled class file. – Mark Peters Jun 17 '10 at 18:14
11

Using the following external tool configuration we can view java byte code in eclipse with the help of javap:

enter image description here

To get the above pop-up select Run option and select External Tools and select External Tools configuration...

enter image description here

${system_path:javap} is used to locate javap in the JDK used by the Eclipse. You can use an absolute path to javap instead.

${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.

Among the arguments passed to javap:

  • bin is the default output folder for Eclipse projects. Change this to build/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.

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.

Source


Dr. Garbage Tools is a suite of Eclipse Plugins released under Apache Open Source license.

Install Bytecode Visualizer using Eclipse Marketplace.

To view a bytecode:

  1. Right click on .java file, select open with and select other you will get the following popup:
    enter image description here

  2. Search "Bytecode Visualizer" and select the option it opens the file as follows there you can find Bytecode option as shown:
    enter image description here


Enhanced Class Decompiler

"Window > Preferences > General > Editors > File Associations"

Change default to your for both .class association.

"*.class" : "Class Decompiler Viewer" is selected by default.

"*.class without source" : "Class Decompiler Viewer" is selected by default.

enter image description here

Premraj
  • 72,055
  • 26
  • 237
  • 180
  • It gives the following error: "No repository found at http://sourceforge.net/projects/drgarbagetools/files/eclipse/4.5/stable/." – alper Mar 06 '17 at 09:32
  • The External Tool setup for javap works great for top-level classes, however it can't find nested classes. – James Apr 21 '18 at 05:25
  • 1
    Add the switch `-XDinner` to also show nested classes. – James Apr 21 '18 at 05:49
9

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

alex2k8
  • 42,496
  • 57
  • 170
  • 221
3

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Or when you have the cursor over the type in Java code (alternatively CTRL+click). Note that Eclipse doesn't actually decompile anything. You just associate a source source with a binary source. The source you see might have absolutely nothing to do with the compiled class file. The source for `BufferManagerWriteCollect` happens to be included in the JDK. Try opening pretty much anything in the sun.** subpackages and you'll see what I mean. – Mark Peters Jun 17 '10 at 16:11
  • @Mark: I agree, even if it isn't exactly a... "combination of keys" ;) But again, neither is my F3 suggestion! – VonC Jun 17 '10 at 16:14
2

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

JBert
  • 3,311
  • 24
  • 37
1

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

Arne Deutsch
  • 14,629
  • 5
  • 53
  • 72
0

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.

HesNotTheStig
  • 549
  • 1
  • 4
  • 9