newspaint

Documenting Problems That Were Difficult To Find The Answer To

Looking Inside A Java Jar File

Here are some simple scripts for inspecting a .jar file.

Listing All Classes and Methods in a Jar

I have found this very useful when given a .jar without any other documentation and have been expected to use it to interface to a proprietary system. Being given the classes and methods along with the method signatures (including expected parameter types) has been a life-saver.

#!/bin/bash

JAR_TO_INTERROGATE=$1
if [ -z "$JAR_TO_INTERROGATE" ]; then
  echo "Please provide JAR file name to interrogate"
  exit 1
fi

CLASSES_OF_JAR=$(jar tf "$JAR_TO_INTERROGATE" |grep class |sed 's/.class//g')

javap -classpath "$JAR_TO_INTERROGATE" -s $CLASSES_OF_JAR

This utility can be used as follows:

user@host:~$ interrogate_jar.sh junit-4.10.jar
Compiled from "ActiveTestSuite.java"
class junit.extensions.ActiveTestSuite$1 extends java.lang.Thread{
final junit.framework.Test val$test;
  Signature: Ljunit/framework/Test;
final junit.framework.TestResult val$result;
  Signature: Ljunit/framework/TestResult;
final junit.extensions.ActiveTestSuite this$0;
  Signature: Ljunit/extensions/ActiveTestSuite;
junit.extensions.ActiveTestSuite$1(junit.extensions.ActiveTestSuite, junit.framework.Test, junit.framework.TestResult);
  Signature: (Ljunit/extensions/ActiveTestSuite;Ljunit/framework/Test;Ljunit/framework/TestResult;)V
public void run();
  Signature: ()V
}

Compiled from "ActiveTestSuite.java"
public class junit.extensions.ActiveTestSuite extends junit.framework.TestSuite{
public junit.extensions.ActiveTestSuite();
  Signature: ()V
public junit.extensions.ActiveTestSuite(java.lang.Class);
  Signature: (Ljava/lang/Class;)V
public junit.extensions.ActiveTestSuite(java.lang.String);
  Signature: (Ljava/lang/String;)V
public junit.extensions.ActiveTestSuite(java.lang.Class, java.lang.String);
  Signature: (Ljava/lang/Class;Ljava/lang/String;)V
public void run(junit.framework.TestResult);
  Signature: (Ljunit/framework/TestResult;)V
public void runTest(junit.framework.Test, junit.framework.TestResult);
  Signature: (Ljunit/framework/Test;Ljunit/framework/TestResult;)V
synchronized void waitUntilFinished();
  Signature: ()V
public synchronized void runFinished();
  Signature: ()V
}
  ...

Showing the Java Disassembly for all Methods in a Jar

#!/bin/bash

JAR_TO_INTERROGATE=$1
if [ -z "$JAR_TO_INTERROGATE" ]; then
  echo "Please provide JAR file name to interrogate"
  exit 1
fi

CLASSES_OF_JAR=$(jar tf "$JAR_TO_INTERROGATE" |grep class |sed 's/.class//g')

javap -classpath "$JAR_TO_INTERROGATE" -c $CLASSES_OF_JAR

This utility can be used as follows:

user@host:~$ disassemble_jar.sh junit-4.10.jar
Compiled from "ActiveTestSuite.java"
class junit.extensions.ActiveTestSuite$1 extends java.lang.Thread{
final junit.framework.Test val$test;

final junit.framework.TestResult val$result;

final junit.extensions.ActiveTestSuite this$0;

junit.extensions.ActiveTestSuite$1(junit.extensions.ActiveTestSuite, junit.framework.Test, junit.framework.TestResult);
  Code:
   0:	aload_0
   1:	aload_1
   2:	putfield	#1; //Field this$0:Ljunit/extensions/ActiveTestSuite;
   5:	aload_0
   6:	aload_2
   7:	putfield	#2; //Field val$test:Ljunit/framework/Test;
   10:	aload_0
   11:	aload_3
   12:	putfield	#3; //Field val$result:Ljunit/framework/TestResult;
   15:	aload_0
  ...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: