Friday, November 02, 2007

Recover A Source Tree From Java Classes

Say you need to make changes to an ancient Java application you can't find the source for. You can typically decompile a .class file to a .java file really easily by using the incredibly awesome Jad Decompiler.

If you have a whole tree full of .class files you can convert them all to .java files with the handy commands:

 cd ~/directory-that-has-start-of-class-tree
 mkdir src
 find -name '*.class'  -exec jad -s .java -r -d src {} \;

And you're done and quite possibly a hero for getting back the MIA .java files.

2 comments:

  1. I once lost a whole bunch of source code due to an errant del *.* command. But I still had the compiled classes.
    So I used Jad to get my source back.

    It worked wonderfully....but I neglected to remove the glaring "THIS CODE HAS BEEN DECOMPILED" notices inserted into the code base.

    Then a coworker approached me and delicately tried to explain that stealing source isn't a good idea for our app. Instead of being upset they would hurl such an accusation at me, I was complimented when they explained that the code structure was so nice that they were surprised I wrote it at all.

    Wait. Maybe I should be doubly insulted....

    ReplyDelete
  2. Nice :-).

    Frankly, I don't think your a real java programmer until you have a story that involves deleting a massive amount of source code and using a decompiler to recover your work.

    Hmmm, next time I need to put an ad for a programmer, I'm so going to include that under the list of requirements.

    ReplyDelete