Find a jar file given the class name?

Viewed 58045

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, given "com.ibm.websphere.security.auth.WSSubject", how do you track down the appropriate jar file? ("google" is not the answer I'm looking for!)

The java docs do not give any hint of the jar file, and obviously the names of the jar files themselves offer no clue.

There must be a 'search local jars', or some sort of 'auto-resolve dependencies', trick in the java world. Ideally, I'm looking for the 'official' way to do this. I happen to be on a windows machine without cygwin.

18 Answers

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 <grep RegEx to look for in contents of jar> [<top-of-folder-hierarchy> 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

in Intellij Idea

  1. on your class press ctrl+B and after that you can find the jar file.
  2. on project bar press scroll from source.
  3. you can see the jar file contains the class.

enter image description here

Related