Decompiling Java Archive Files

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.

  1. Download 7zip(or winRar) and JAD as explained earlier.
  2. Extract the .jar folder to any location you desire. (Only extracting the .class file your after may work also)
  3. 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)
  4. Double-click on the JAD executable and observe the command line briefly open, run some code, and close again.
  5. Now open command prompt and cd to the directory containing the class you want to decompile. (C:> cd “absolute path to folder containing file”)
  6. 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
  7.  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!


CiviCRM on a localhost with WordPress or Drupal

Although the respective information on how to configure WordPress or Drupal on a localhost exists online, as well as how to set up CiviCRM with either of the two CMS’s, I’ve put together a guide so that you can get all the information from the one place. If you are here only to set up WordPress or Drupal locally this guide will also prove useful.  Note that alternative methods exist but this one is quite straightforward. Also don’t be deterred by the amount of programs mentioned below, there is such a huge amount of work removed from this process by the interfaces and automated setups they provide. This is a guide for Windows users only.

  1. First thing to do is download WAMPSERVER which provides an Apache Server, MySQL database and PHP all automatically configured setting the foundation for the local hosting environment. I chose the 32-bit-Apache 2.2.22 – Mysql 5.5.24 – PHP 5.3.13 version.
  2. Next run WAMP and find the icon in the bottom right of the Windows taskbar. You may need to click to show hidden icons. Left-click the icon and navigate to Apache>Apache Modules and ensure that the rewrite module is ticked.
  3. Next left-click the WAMP icon again and go to phpMyAdmin. This provides a convenient interface for your MySQL databases. When it opens in a browser go the database tab, enter a new database name and click create database leaving all the default settings. For convenience call this database drupal or wordpress.
  4. Download the latest version of Drupal or WordPress and extract once for zip or twice for tar.gz files using a program such as 7zip or just right-click the download and unzip.
  5. Open the folder which will be called Drupal[version] or WordPress[version] and take out the single folder inside named Drupal or WordPress. Place this in the ‘C:\wamp\www\’ directory. You can now change the name of this folder if you wish as this will be part of the url used to navigate to the content in the browser.
  6. Open a  browser window and type localhost/foldername/ and you should be redirected to either the Drupal or WordPress install setup. Follow the instructions and enter the database you had previously created. The username is ‘root’ by default and leave the password blank. That’s your local WordPress or Drupal site ready to go!
  7. Download the latest version of Civi CRM from the sourceforge page selecting ‘civicrm-[version]-drupal.tar.gz’ or ‘civicrm-[version]’ and extract/unzip. Again open the folder displaying the version number and take the civicrm folder from within.
  8. Place this folder in ‘C:\wamp\www\foldername\wp-content\plugins’ for WordPress or ‘C:\wamp\www\foldername\modules’ for Drupal.
  9. For WordPress, enter localhost/foldername/ you browser url and login to your WordPress site. Go to plugins and activate the CiviCRM plugin then go to settings>CiviCrm Installation. For Drupal enter ‘http://localhost/foldername/modules/civicrm/install/index.php’ into your browser url.
  10. The installation page prefills some fields. In Drupal the second database must be the one you created with root as the username and the password left empty. As you are logged into WordPress already this is not required. The other database for both will be created upon installation. Call it civicrmwordpress or civicrmdrupla for convenience and give it username root and an empty password for now also. Tick load sample data unless you’d prefer not to. At this stage almost all of the requirements will be OK if you have entered the correct database, username and password. However ‘Does MySQL thread_stack meet minimum (192k)’ will likely not be OK after setting the databases. You must  click the WAMP icon in your Windows taskbar and click restart all services. Then click re-check requirements back in the installation Window in which case all requirements should be OK. You’re ready to install will appear at the top of the page and click Check Requirements and Install CiviCRM.

Running from a localhost is a great free method to experiment with any of the programs mentioned above. Perhaps create a full-backup of your online site (to be covered in a future post) for a piece of mind or to try some plugins or edit some code. If your considering paying for a account, this is a great free method to take every aspect of the CMS for a test run.Perhaps you only want CiviCRM running on a local computer in the office to store important data about customers or employees. Having access to your server setting’s and learning how all of the processes required for a CMS to work make this a worthy venture alone.