Thursday, July 5, 2012

How to search class in jar files using Jarscan

Suppose you are working on a project and want to include a library jar file which has specific java class. But at the same time you don't have much idea about which jar file to include and you are given with a lot of jar files to choose from. How would you figure out which jar file is the most suitable one.
Well here comes Jarscan to rescue. Jarscan is an executable jar file which scans the jar/zip file and lists jar files which contains the desired class.

Prerequisite:
  • Java 1.4 or higher version should be installed in your system. If your system doesn't have it then you can download it from here.  
  • Jarscan which can be downloaded from here.
Here is the command example below:
java -jar jarscan.jar -help                  #This provides help, how to use jarscan.
java -jar jarscan.jar                        #This works as the command above.
#The following command looks for Activity class root from current directory. 
java -jar jarscan.jar -dir . -class Activity  
You can even search classes through database of libraries online hereIf you want more information about Jarscan you can find it here.

You can try the online services here.

Java class file decompilation

What if you are interested to know more about the class methods, variable, etc. and the only thing that you have is the class file. Don't worry, there is a way to know about these methods, variables etc. even without source file. Javap can help you parsing the class file and providing the information about the  desired class. Javap executable comes with java package. Most of the people either aren't aware of it or don't use it because they rely more on IDE(e.g Eclpse)/build system. Here we are going to use the javap command option for the following class.
 
package com.rakesh.simplewidget;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;


public class ConnectivityReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  NetworkInfo info = (NetworkInfo)intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
  
  if(info.getType() == ConnectivityManager.TYPE_MOBILE){
   
   RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
     R.layout.widget_layout);
   
   if(info.isConnectedOrConnecting()){
    Toast.makeText(context, "Data packet enabled", Toast.LENGTH_SHORT).show();
    Log.d("RK","Mobile data is enabled");
    remoteViews.setTextColor(R.id.BtEnableDisable, Color.GREEN);
    remoteViews.setTextViewText(R.id.BtEnableDisable, "Enabled");
   }else{
    Toast.makeText(context, "Data packet disabled", Toast.LENGTH_SHORT).show();
    Log.e("RK","Mobile data is disconnected");
    remoteViews.setTextColor(R.id.BtEnableDisable, Color.BLACK);
    remoteViews.setTextViewText(R.id.BtEnableDisable,"Disabled");
   }
   
   ComponentName thiswidget = new ComponentName(context, SimpleWidgetAppWidgetProvider.class);
   AppWidgetManager manager = AppWidgetManager.getInstance(context);
   manager.updateAppWidget(thiswidget, remoteViews);
   
  }
 }

}
Compile the java file and then run the commands below.
 
# Sanity check. If you don't get below message after executing javap command
# then include the java installation folder path in system path.
$javap      
No classes were specified on the command line.  Try -help. 
                                                           
$javap -help      #This command lists all the options available with javap
Usage: javap <options> <classes>...

where options include:
   -c                        Disassemble the code
   -classpath <pathlist>     Specify where to find user class files
   -extdirs <dirs>           Override location of installed extensions
   -help                     Print this usage message
   -J<flag>                  Pass <flag> directly to the runtime system
   -l                        Print line number and local variable tables
   -public                   Show only public classes and members
   -protected                Show protected/public classes and members
   -package                  Show package/protected/public classes
                             and members (default)
   -private                  Show all classes and members
   -s                        Print internal type signatures
   -bootclasspath <pathlist> Override location of class files loaded
                             by the bootstrap class loader
   -verbose                  Print stack size, number of locals and args for methods
                             If verifying, print reasons for failure

#This command decompile the ConnectivityReceiver class file
$javap -c ConnectivityReceiver   
Compiled from "ConnectivityReceiver.java"
public class com.rakesh.simplewidget.ConnectivityReceiver extends android.content.BroadcastReceiver{
public com.rakesh.simplewidget.ConnectivityReceiver();
  Code:
   0: aload_0
   1: invokespecial #8; //Method android/content/BroadcastReceiver."<init>":()V
   4: return

public void onReceive(android.content.Context, android.content.Intent);
  Code:
   0: aload_2
   1: invokevirtual #16; //Method android/content/Intent.getExtras:()Landroid/os/Bundle;
   4: ldc #22; //String networkInfo
   6: invokevirtual #24; //Method android/os/Bundle.get:(Ljava/lang/String;)Ljava/lang/Object;
   9: checkcast #30; //class android/net/NetworkInfo
   12: astore_3
   13: aload_3
   14: invokevirtual #32; //Method android/net/NetworkInfo.getType:()I
   17: ifne 144
   20: new #36; //class android/widget/RemoteViews
   23: dup
   24: aload_1
   25: invokevirtual #38; //Method android/content/Context.getPackageName:()Ljava/lang/String;
   28: ldc #44; //int 2130903041
   30: invokespecial #45; //Method android/widget/RemoteViews."<init>":(Ljava/lang/String;I)V
   33: astore 4
   35: aload_3
   36: invokevirtual #48; //Method android/net/NetworkInfo.isConnectedOrConnecting:()Z
   39: ifeq 81
   42: aload_1
   43: ldc #52; //String Data packet enabled
   45: iconst_0
   46: invokestatic #54; //Method android/widget/Toast.makeText:(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;
   49: invokevirtual #60; //Method android/widget/Toast.show:()V
   52: ldc #63; //String RK
   54: ldc #65; //String Mobile data is enabled
   56: invokestatic #67; //Method android/util/Log.d:(Ljava/lang/String;Ljava/lang/String;)I
   59: pop
   60: aload 4
   62: ldc #73; //int 2131099649
   64: ldc #74; //int -16711936
   66: invokevirtual #75; //Method android/widget/RemoteViews.setTextColor:(II)V
   69: aload 4
   71: ldc #73; //int 2131099649
   73: ldc #79; //String Enabled
   75: invokevirtual #81; //Method android/widget/RemoteViews.setTextViewText:(ILjava/lang/CharSequence;)V
   78: goto 117
   81: aload_1
   82: ldc #85; //String Data packet disabled
   84: iconst_0
   85: invokestatic #54; //Method android/widget/Toast.makeText:(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;
   88: invokevirtual #60; //Method android/widget/Toast.show:()V
   91: ldc #63; //String RK
   93: ldc #87; //String Mobile data is disconnected
   95: invokestatic #89; //Method android/util/Log.e:(Ljava/lang/String;Ljava/lang/String;)I
   98: pop
   99: aload 4
   101: ldc #73; //int 2131099649
   103: ldc #92; //int -16777216
   105: invokevirtual #75; //Method android/widget/RemoteViews.setTextColor:(II)V
   108: aload 4
   110: ldc #73; //int 2131099649
   112: ldc #93; //String Disabled
   114: invokevirtual #81; //Method android/widget/RemoteViews.setTextViewText:(ILjava/lang/CharSequence;)V
   117: new #95; //class android/content/ComponentName
   120: dup
   121: aload_1
   122: ldc #97; //class com/rakesh/simplewidget/SimpleWidgetAppWidgetProvider
   124: invokespecial #99; //Method android/content/ComponentName."<init>":(Landroid/content/Context;Ljava/lang/Class;)V
   127: astore 5
   129: aload_1
   130: invokestatic #102; //Method android/appwidget/AppWidgetManager.getInstance:(Landroid/content/Context;)Landroid/appwidget/AppWidgetManager;
   133: astore 6
   135: aload 6
   137: aload 5
   139: aload 4
   141: invokevirtual #108; //Method android/appwidget/AppWidgetManager.updateAppWidget:(Landroid/content/ComponentName;Landroid/widget/RemoteViews;)V
   144: return
}

#This command lists the line number and local variables
$ javap -l ConnectivityReceiver   
Compiled from "ConnectivityReceiver.java"
public class com.rakesh.simplewidget.ConnectivityReceiver extends android.content.BroadcastReceiver{
public com.rakesh.simplewidget.ConnectivityReceiver();
  LineNumberTable: 
   line 15: 0

  LocalVariableTable: 
   Start  Length  Slot  Name   Signature
   0      5      0    this       Lcom/rakesh/simplewidget/ConnectivityReceiver;


public void onReceive(android.content.Context, android.content.Intent);
  LineNumberTable: 
   line 19: 0
   line 21: 13
   line 23: 20
   line 24: 28
   line 23: 30
   line 26: 35
   line 27: 42
   line 28: 52
   line 29: 60
   line 30: 69
   line 32: 81
   line 33: 91
   line 34: 99
   line 35: 108
   line 38: 117
   line 39: 129
   line 40: 135
   line 43: 144

  LocalVariableTable: 
   Start  Length  Slot  Name   Signature
   0      145      0    this       Lcom/rakesh/simplewidget/ConnectivityReceiver;
   0      145      1    context       Landroid/content/Context;
   0      145      2    intent       Landroid/content/Intent;
   13      132      3    info       Landroid/net/NetworkInfo;
   35      109      4    remoteViews       Landroid/widget/RemoteViews;
   129      15      5    thiswidget       Landroid/content/ComponentName;
   135      9      6    manager       Landroid/appwidget/AppWidgetManager;


}

#This command prints internal type signatures 
$ javap -s ConnectivityReceiver   
Compiled from "ConnectivityReceiver.java"
public class com.rakesh.simplewidget.ConnectivityReceiver extends android.content.BroadcastReceiver{
public com.rakesh.simplewidget.ConnectivityReceiver();
  Signature: ()V
public void onReceive(android.content.Context, android.content.Intent);
  Signature: (Landroid/content/Context;Landroid/content/Intent;)V
}

#This command shows package/protected/public classes and members (default) 
$ javap -package ConnectivityReceiver 
Compiled from "ConnectivityReceiver.java"
public class com.rakesh.simplewidget.ConnectivityReceiver extends android.content.BroadcastReceiver{
    public com.rakesh.simplewidget.ConnectivityReceiver();
    public void onReceive(android.content.Context, android.content.Intent);
}


#Similar way you can also use -public, -protected -private etc. options


Related topic:
Android/java source code browsing

Your valuable comments are always welcomed. It will help to improve my post and understanding.

"By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."
By : Confucius