Find a jar file given the class name?

后端 未结 17 2073
无人共我
无人共我 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: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.

提交回复
热议问题