From 05/10/2012. See original post on another blog here.
Just today, I came upon the mystifying entity that is the .jar file. Upon Google searching it towards oblivion, it proved not so easy to find a document comprehensively explaining how to open this file and view the java code trapped within. So that I may hit upon a similar situation that you might find yourself in, I want to go through the steps towards my resolution. Firstly, a bit of background as to what I was trying to accomplish, however, you can skip this paragraph if you wish to get on with things. As part of my thesis, I am experimenting with Csoundo, a library for Processing, which calls processes from the audio synthesis programming language that is Csound. In a effort to update the Csoundo java files to work with the latest version of Processing, amongst other things, I wanted to investigate the classes which are called by Csoundo for operation, processing.core.PApplet in particular. As I found, the core class I was looking for turned out to be a java archive file.
A java archive file (.jar) is a precompiled and zipped ‘folder’ containing many java classes. Programmers employ it for a more efficient distribution of completed java-based applications. The problem is that one cannot open it and view the contents without utilizing the right programs. To begin with, I downloaded 7zip (alternatively use winrar) which allowed me to navigate to the .jar file and open it to view its contents. Two folders will be viewable, one named META-MF, which contains the manifest file that is, basically, a set of instructions as to how the .jar file will be used. The other folder contains all the java classes that you are after. Extracting the class I was looking for proved not to be sufficient enough as I still had a compiled file that I could not open and view the java code for. I then read about JAD, a java decompiler of which you can download a mirror version. Downloading the version ‘Windows 9x/NT/2000 on Intel platform,’ (for windows) you will get a zip file that needs to be extracted. A JAD executable and a ReadMe file are contained within the zip. Upon running the JAD executable, the command line briefly appeared and ran some lines before closing again. I mistakenly believed that my download was not compatible with my machine as when I opened the command line and ran the necessary instructions from the ReadMe file it did not work, with the command line stating ” ‘jar’ is not recognised as an internal or external command, operable program or batch file.” At this stage I was so close to the solution albeit two problems which I resolved later in my conquest. However, I now went on in the search of other solutions.
It is here I went a bit off track and once again, you can skip the following paragraph if you want to read on towards the solution. In pursuit of a solution, I came upon recommendations to download the JDK to open .jar files and while I was at it, I downloaded the NetBeans IDE in the process. Choosing open file in the toolbar menu, I successfully opened the .jar file, which allowed me to navigate into the ‘packages’ or folders mentioned earlier. It was now that by clicking on the class I desired to view, it actually opened successfully and I could view all of the code of the once compiled class. Hey presto I thought, but, unfortunately, a problem remained. I was not able to view any of the (many) methods within the class and the spaces between the curly brackets of each were replaced with a small white text box stating that it was ‘Compiled Code.’ Now I’m sure there is a way to use the JDK or NetBeans to decompile .jar files, perhaps using plugins, but at this stage I moved on towards other solutions. I read a lot about a plug-in for Eclipse that allowed the decompiling of java files of which I went ahead and begin downloading but as this was happening, I came upon a webpage explaining the proper way to use JAD. It turns out there was no need for all the big downloads and that things are actually a lot simpler. What follows is my amended version of the instructions freddieMaize so usefully posted.
- Download 7zip(or winRar) and JAD as explained earlier.
- Extract the .jar folder to any location you desire. (Only extracting the .class file your after may work also)
- Open the extracted folder and place the JAD executable into the same directory or folder as the .class that you want to decompile and view. (This is important as it addresses the problem the command line stated to me earlier)
- Double-click on the JAD executable and observe the command line briefly open, run some code, and close again.
- Now open command prompt and cd to the directory containing the class you want to decompile. (C:> cd “absolute path to folder containing file”)
- Now run the following command that decompiles the .class file you want to view and saves it as a .java file in the same directory. (This addresses the second problem I was having earlier which is to perform the following command on the extracted class, not on the .jar file) C:\pathtofile> jad -sjava ClassName.class
- That’s it! You can open and view the newly created .java file with notepad or your favourite IDE.
Now I know there are probably many other and more efficient ways of completing this task but for me these steps were quick and easy, requiring only two small downloads, and most importantly allow me to finally view all the .jar files I need to get at for my thesis!