Decompile Java using C#

On February 4, 2013, in C#, Java, Programming, Software Development, by LuCuS

Java LogoThroughout my programming career, I have always tried to write my code with the intent to reuse it where possible. Most programmers prefer to utilize third-party libraries to keep from having to rewrite code. For me, I like to write everything from scratch at least once so that I understand what exactly is going on and how everything works. After that, I will typically export reusable code as a library which I can reuse later on. I prefer not to use third party libraries for a couple of reasons. The first reason I don’t like using third party libraries is because I have been bitten several times from version incompatibilities. For whatever reason, not all developers keep their libraries up to date making it difficult for me to upgrade my apps if I’m using something that someone else built and is now obsolete. Another reason I prefer not to use third party libraries is because of licensing issues. I know there are a lot of great license models out there. But, I still don’t want to get mixed up in all of that, especially when I decide to take something I’ve written for myself and make it commercial.

Even though I try my best to write my code so that I can reuse it later on, that isn’t always the case. Over the weekend, I was working on a project that required a functionality I had written before in another project. However, the piece of code I was needing did not get moved into its own library project as I didn’t feel like I would ever need that code again. Either that or I was just in a hurry when I wrote it the first time that I neglected to realize its possible importance. To make things worse, I no longer have the source code for the original project that included the code I needed. Therefore, I was left with three options. The first option was to completely rewrite the code from scratch. Being that this particular code included a lot of work and spanned across several classes, I didn’t want to reinvent the wheel nor did I want to lose any time rewriting / debugging everything all over again. The second option was to import the entire original project into my new project and reference only the pieces I needed. The original project was huge and included a ton of stuff I didn’t need for my current project. So, this too was not a viable option. The third option, and option I decided to go with, was to simply decompile the original project and extract only the key features I needed.

To do this, I remember stumbling across a tool a while back called “JAD” (short for JAva Decompiler). JAD is a command line driven tool that can decompile any .class file back into its readable .java format. It has been so long since I have used JAD that I couldn’t remember the proper syntax for all of the commands. Even though JAD spits out its own usage report, for some reason I just didn’t feel like working with the command line this weekend. So, I did what any geek like myself would do and I wrote a C# interface that can interact with the command line utility.

To keep things simple, I called my JAD wrapper JAD# (JAD -sharp) since I decided to write it in C#. To use the wrapper interface, you can click the “Browse” button at the top, navigate to, and select any CLASS file. Since my purposes for doing this involved decompiling a previous project of mine, I had to first extract the contents of the JAR file that included the code I needed to get at. For those of you that don’t know, JAR files are basically ZIP files. So, you can use any unzipping utility such as 7-zip to extract JAR files into an exploded format on your file system. After you have selected the CLASS file you wish to decompile, all you have to do is click the “Decompile” button. If you do not want to use the Browse and Decompile buttons, you can simply drag the CLASS file directly onto the file name text box and it will automagically invoke the Decompile button for you. Once the Decompile button has been invoked, the tool will call out to the JAD utility executing the necessary commands and the final decompiled source code will be displayed in the window as shown below.


Since I couldn’t remember the exact locations of all the files I needed from the previous project, having the decompiled code displayed in a window allowed me to quickly and easily flip thru each of the CLASS files until I found all of the files I needed. As I found each file I needed, I could highlight and copy the necessary pieces directly from the display window and paste it into my new project. For instances where I needed entire classes, I added a “Save” button. When you click the Save button, a save dialog window is displayed. I have the save dialog defaulting the file name to the name of the CLASS file you are decompiling, but with the “.java” extension instead of “.class”. It also defaults to the same folder that the “.class” file is located.

For those of you that are interested, I have provided the entire Visual Studio solution which you can download from If you don’t care about the source code for the user interface, you can still download the same ZIP file where you can run the utility by executing JAD.exe from the bin/Debug folder. I renamed the original JAD utility to “jadit.exe” which you can execute from the command line if you want to see any of the additional commands I didn’t add support for in the interface. Here is the code I used for interacting with the JAD command line tool.

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("jadit.exe", "-p \"" + filename + "\"");

procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
string result = proc.StandardOutput.ReadToEnd();
txtConsole.Text = result;

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.

Related Posts

Tagged with:  

Leave a Reply