Find a Class Somewhere Inside Dozens of Jar Files

Find a class somewhere inside dozens of JAR files?

Eclipse can do it, just create a (temporary) project and put your libraries on the projects classpath. Then you can easily find the classes.

Another tool, that comes to my mind, is Java Decompiler. It can open a lot of jars at once and helps to find classes as well.

How to know which classes inside a .jar file are referenced?

Check out ProGuard which is an obfuscator that will list code and classes that are not used. Obfuscating itself usually results in a smaller foot print.

ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or for Java Micro Edition.

I'm trying to search a '.class' file within jar files using Python

Jars are similar to Zip archives. To scan through jar files you can use the Python module zipfile to get its contents list or you can even read the contents. You can get the list of contents in jar using Zipfile.namelist() method, then use this list to check whether the file you are searching for is present or not.

Here is a sample code which gets the list of files present in a jar.

import zipfile
archive = zipfile.ZipFile('<path to jar file>/test.jar', 'r')
list = archive.namelist()

if you will run this in comaand line or terminal you will get output like:

['file1.class', 'file2.class' ]

where file1 and file2 are the two .class files which I had in my jar file.

JarScan, scan all JAR files in all subfolders for specific class

Something like:

find . -name '*.jar' | while read jarfile; do if jar tf "$jarfile" | grep org/jboss/Main; then echo "$jarfile"; fi; done

You can wrap that up like this:

jarscan() {
pattern=$(echo $1 | tr . /)
find . -name '*.jar' | while read jarfile; do if jar tf "$jarfile" | grep "$pattern"; then echo "$jarfile"; fi; done
}

And then jarscan org.jboss.Main will search for that class in all jar files found under the current directory

To see method in .class file embedded in jar file | is it possible ?

Extract the class from jar file and then run

unzip Classes.jar
find . -name '*.class' | xargs javap -p > classes.txt

The classes.txt file will have all information about the classes inside jar. You can search it for a method.

Find a jar file given the class name?

Save this as findclass.sh (or whatever), put it on your path and make it executable:

#!/bin/sh
find "$1" -name "*.jar" -exec sh -c 'jar -tf {}|grep -H --label {} '$2'' \;

The first parameter is the directory to search recursively and the second parameter is a regular expression (typically just a simple class name) to search for.

$ findclass.sh . WSSubject

The script relies on the -t option to the jar command (which lists the contents) and greps each table of contents, labelling any matches with the path of the JAR file in which it was found.



Related Topics



Leave a reply



Submit