The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Decompiling Java
by Christopher Diggins
June 22, 2005
Here is some Java code which uses the BCEL to convert a class file into java byte code.


I have been playing with Java byte code lately and I've just discovered the awesome Byte Code Engineering Library (BCEL). I feel like a kitten with a ball of yarn. Here is the source for a simple decompiler:


import java.util.Iterator;
import org.apache.bcel.*;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;
import org.apache.bcel.Repository;

public class Decompiler
  public static void main(String[] argv)
      // Load the class from CLASSPATH.
      JavaClass       clazz   = Repository.lookupClass(argv[0]);
      Method[]        methods = clazz.getMethods();
      ConstantPoolGen cp = new ConstantPoolGen(clazz.getConstantPool());
      InstructionFactory f = new InstructionFactory(cp);

      for(int i=0; i < methods.length; i++)
        Method m = methods[i];
        MethodGen mg = new MethodGen(m, clazz.getClassName(), cp);
        decompileMethod(mg, f);
    catch(Exception e)

  private static final void decompileMethod(MethodGen mg, InstructionFactory f)
    InstructionList   il    = mg.getInstructionList();
    int               count = 0;

    System.out.println("Method " + mg.getName());
    InstructionHandle ih = il.getStart();
    while (ih != null) {
      ih = ih.getNext();

    il.dispose(); // Reuse instruction handles
I am planning next on a writing a ClassLoader which hacks code as it loads it. This is so much fun!

Talk Back!

Have an opinion? Readers have already posted 2 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at

This weblog entry is Copyright © 2005 Christopher Diggins. All rights reserved.

Sponsored Links


Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use