Friday, November 28, 2008

Using VIM in troubleshooting JAVA application

After trying many Text Editor available on Windows as TexPad, UltraEdit, Wordpad, Notepad, etc. , finally I decided to use vim, an enhanced version of unix vi. Vim is never been an easy choice but once you master it, it is wonderful, it give you high productivity. Also it is free.
Some basic vi commands can be found in http://www.cs.uiuc.edu/class/fa07/cs225/calendar/vim.pdf.
During my daily work, I often have to troubleshoot complex JAVA application. Sometime I get exception with full JAVA stack printout, but the application is provided by vendor without source code so there is very difficult to find a root cause.
There is a tool called JAD that can decompile JAVA classes. Because my primary editor is VIM so I decided to integrate the JAD into VIM. Googling a while, I found vim JAD plugin, that display decompiled java class whenever we open the class file. However JAVA application(s) are mostly provided in form of several jar, zip, ear package so it is not so convenient.
There is luckily zip plugin that is part of vim installation and can browse zip, jar, ear package and display content of selected file inside the package when we hit enter. Modify just few lines of this plugin, I am able to browse content of jar, zip file and view a decompiled inside JAVA class.
The steps is as follows (for version 7.2, that was installed in D:\Vim\)

1. open D:\Vim\vim72\autoload\zip.vim and change
fun! zip#Read(fname,mode)
...
  exe "silent r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1)
...
endfun
to
fun! zip#Read(fname,mode)
...
 if fname =~ '.class$'
   exe "silent r! ".g:zip_unzipcmd." -o -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1)
   exe "silent r! jad -lnc -p ".s:Escape(fname,1)
 else
   exe "silent r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1)
 endif
...
endfun
2. download unzip.exe, jad.exe and put them in the PATH
3. start vim and open a jar file, a list of files inside the jar file will be displayed
4. select one class file and hit ENTER The '-lnc' option of JAD display line number of original source code as comment on the left side of decompiled code so it can be used to identify which part of code cause an exception.

2 comments:

sethu madhav said...

Awesome , works like a charm

Anonymous said...

Awesome, thanks!