Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List contents of multiple jar files

Tags:

I am searching for a .class file inside a bunch of jars.

jar tf abc.jar  

works for one file. I tried

find -name "*.jar" | xargs jar tf 

prints nothing. The only solution I can think of, is unzip all, then search. Is there a better way? I'm on LUnix.

Edit: When scanning many jars, it is useful to print the jar file name along with the class. This method works well:

find . | grep jar$ | while read fname; do jar tf $fname | grep SchemaBuilder && echo $fname; done 

Sample output produced:

  1572 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder$1.class   1718 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder$2.class  42607 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder.class ./XmlSchema-1.3.2.jar   1572 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder$1.class   1718 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder$2.class  42607 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder.class ./XmlSchema.jar 
like image 937
user443854 Avatar asked Oct 14 '11 15:10

user443854


People also ask

How do I find a class file in a bunch of JARs?

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 do I view the contents of a JAR file in Windows?

Right-click on the JAR file and select Extract All. View the contents of the open JAR file on the file system.


1 Answers

You need to pass -n 1 to xargs to force it to run a separate jar command for each filename that it gets from find:

find -name "*.jar" | xargs -n 1 jar tf 

Otherwise xargs's command line looks like jar tf file1.jar file2.jar..., which has a different meaning to what is intended.

A useful debugging technique is to stick echo before the command to be run by xargs:

find -name "*.jar" | xargs echo jar tf 

This would print out the full jar command instead of executing it, so that you can see what's wrong with it.

like image 195
NPE Avatar answered Nov 30 '22 05:11

NPE