Find a jar file given the class name?

后端 未结 17 1922
无人共我
无人共我 2020-11-29 18:46

This must be a very basic question for Java developers, but what is the best way to find the appropriate jar file given a class name?

For example, give

相关标签:
17条回答
  • 2020-11-29 19:39

    if you are still searching for WSSubject, then jar is wssec.jar. WSSecurityException class inside sas.jar

    0 讨论(0)
  • 2020-11-29 19:40

    In Windows, run cmd.exe and type:

      for %i in (*.jar) do @jar tvf %i | find "/com/company/MyClass.class"
    

    The jars would have to be in the current directory. For also has a /R option which takes a directory and lets you search recursively.

    If Jar.exe isn't in your path, you can do something like @C:\jdk\bin\jar.exe.

    0 讨论(0)
  • 2020-11-29 19:44

    I recommend using Maven, Eclipse and m2eclipse.

    Step 1 - add specific import

    alt text

    Step 2 - find and download (automatically) desired jar

    alt text

    0 讨论(0)
  • 2020-11-29 19:45

    If the grep on your system (e.g. Solaris) doesn't have -H and --label as used in Dan Dyer's example, you can use:

    find . -name '*.jar' -type f | xargs -i bash -c "jar -tvf {}| tr / . | grep WSSubject && echo {}"
    
    0 讨论(0)
  • 2020-11-29 19:47

    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.

    0 讨论(0)
提交回复
热议问题