Find a jar file given the class name?

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

    Building up on Dan's excellent answer, the following script solves the problem of mangled output in case some of the jars are actually broken symlinks (while at the same time not skipping proper symlinks) It also searches in the current directory if no argument is provided.

    #!/usr/bin/env bash
    
    if [[ ($# -ne 1) && ($# -ne 2) ]]
    then
        echo "usage is $0  [ or, if missing, current dir]"
    else
        REG_EXP=$1
        DIR=${2:-.}
        if [ ! -d $DIR ]; then
            echo "directory [$DIR] does not exist";
            exit 1;
        fi
        find "$DIR" -name "*.jar" -exec sh -c '
        (test -e {})
        exitStatus=$?
        if [ "$exitStatus" -eq 0 ]; then # this is done to avoid broken symlinks
            jar -tf {}|grep -i -H --label {} '$REG_EXP'
        fi
    ' \;
    fi
    

提交回复
热议问题