3

I am trying to find a JAR file which contains my class using below command given in this link : Find a jar file given the class name?

find . -name "*.jar" -exec sh -c 'jar -tf {}|grep -H --label {} GenericClassLoader' \;

but I am getting error as :

java.util.zip.ZipException: error in opening zip file
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:131)
        at java.util.zip.ZipFile.<init>(ZipFile.java:92)
        at sun.tools.jar.Main.list(Main.java:997)
        at sun.tools.jar.Main.run(Main.java:242)
        at sun.tools.jar.Main.main(Main.java:1167)

Please help me in understanding the command and also how solve this error. I am using bash shell.

Community
  • 1
  • 1
chaitanya
  • 701
  • 4
  • 14
  • 26

1 Answers1

4

There is something in your filesystem, which ends with *.jar but which isn't a valid archive. To debug this, add -print to the find command:

find . -name "*.jar" -print -exec ...

This will make find print the name of the file before it tries to execute the sh command.

Explanation:

  • find .: Find all files and folder in the current directory
  • -name "*.jar" whose name ends with .jar
  • -exec and execute the following command for each of them
  • sh -c Create a new shell and execute ...
  • jar -tf {} Test a JAR archive. This (also) prints a list of files in the archive find will replace the {} with the path of the archive it found
  • | pipe the result of jar -tf into ...
  • grep search the input for ...
  • -H Print the filename for each match
  • --label {} Since we're reading from stdin, grep should use {} (again replaced by find) as the filename
  • GenericClassLoader search for this string in the output of jar -tf
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thanks a lot Aaron, is there a way for hiding those error messages and displaying only correct output? I am able to find the jar file from the output but I have almost 100 error messages – Chaitanya May 03 '13 at 10:18
  • Add `2>&/dev/null` between `{}` and `|`: `'jar -tf {} 2>&/dev/null | grep` – Aaron Digulla May 03 '13 at 10:31
  • I think I am missing something here, I am using this command: `find . -name "*.jar" -exec sh -c 'jar -tf {} 2>&/dev/null |grep -H --label {} GenericClassLoader' \;` but I am getting error as `sh: /dev/null: ambiguous redirect` , please help me on this. – chaitanya May 03 '13 at 11:09
  • try `bash -c` instead of `sh -c` – Aaron Digulla May 03 '13 at 12:34
  • I am getting same error, I am using this command : `find . -name "*.jar" -exec bash -c 'jar -tf {} 2>&/dev/null |grep -H --label {} GenericClassLoader' \;` , still I am getting error: `bash: /dev/null: ambiguous redirect` – chaitanya May 03 '13 at 13:10
  • :-/ it works for me (cut&paste). My guess is that you have odd file names. Really add `-print` to see for which files it prints errors. I think there are files with spaces in the name. if so use `"{}"` instead of `{}` (i.e. add quotes) – Aaron Digulla May 03 '13 at 15:35
  • It is resolved now, after changing my command to `find . -name "*.jar" -exec bash -c 'jar -tf {} 2> /dev/null |grep -H --label {} GenericClassLoader' \; ` Thanks a lot for your continuous supprot. – chaitanya May 03 '13 at 17:22