List contents of multiple jar files
Solution 1
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.
Solution 2
I faced a similar situation where I need to search for a class in a list of jar files present in a directory. Also I wanted to know from which jar file the class belongs to. I used this below code snippet(shell script) and found out to be helpful. Below script will list down the jar files that contains the class to be searched.
#!/bin/sh
LOOK_FOR="codehaus/xfire/spring"
for i in `find . -name "*jar"`
do
echo "Looking in $i ..."
jar tvf $i | grep $LOOK_FOR > /dev/null
if [ $? == 0 ]
then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
done
Shell script taken from : http://alvinalexander.com/blog/post/java/shell-script-search-search-multiple-jar-files-class-pattern
Solution 3
You can also use -exec
option of find
find . -name "*.jar" -exec jar tf {} \;
Solution 4
Here's what I use in Cygwin. It supports section headers per jar file as requested above.
find . -name "*.jar" \
-exec echo ======\ {}\ ====== \; \
-exec /c/Program\ Files/Java/jdk1.7.0_45/bin/jar.exe tf {} \; | less
For *nix, drop the ".exe":
find . -name "*.jar" \
-exec echo ======\ {}\ ====== \; \
-exec jar tf {} \; | less
Related videos on Youtube
user443854
Updated on November 28, 2020Comments
-
user443854 over 3 years
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
-
Lucas about 11 yearsYour Edit is exactly what i was looking for. Thanks
-
-
im8bit about 11 yearsExcellent tip with the "echo"
-
Abhijit Gaikwad about 10 yearscan you please modify command find -name "*.jar" | xargs -n 1 jar tf to show which file's contents are being show?
-
mjuarez over 8 yearsPlease try to add more context, not only post a link, since it may go stale in the future.
-
Praveen Baskar over 8 yearsthanks for your inputs piet.t@ and mjuarez@. Updated my answer accordingly. feel free to provide your inputs. thanks!
-
Irina Rapoport over 7 yearsWonder why it's so slow?
-
Angus Grant over 2 years@AbhijitGaikwad I believe I have achieved which you and i wanted with this command for i in $(ls -t $(find . -name '*.jar')); do echo $i && jar tf $i; done