Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a jar file given the class name?

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.

like image 546
Jeffrey Knight Avatar asked Sep 30 '09 19:09

Jeffrey Knight


People also ask

How do I search for a class in a jar file?

this is a very simple and useful tool for windows. A simple exe file you click on, give it a directory to search in, a class name and it will find the jar file that contains that class. Yes, it's recursive.

How can I tell which class a jar was loaded from?

The, the idea is to use find on the root of your classpath to locate all jars, then runs findclass.sh on all found jars to look for a match. It doesn't handle multi-directories, but if you carefully choose the root you can get it to work.

How do I search for a specific jar file in Linux?

find ./ -name "filename" or you can do something like find ./ -name "*. jar" to find all the files with the . jar extension. You can also do find ./ -name "*.


2 Answers

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.

like image 73
Dan Dyer Avatar answered Oct 05 '22 21:10

Dan Dyer


There is no "official" Java way to do this AFAIK.

The way I usually hunt for it is to use find and jar to look through all jar files in a given tree.

> find . -name \*.jar -print -exec jar tf {} oracle/sql/BLOB.class \; ./v2.6.1/lib/csw_library.jar ./v2.6.1/lib/oracle_drivers_12_01.jar oracle/sql/BLOB.class 

If you're on Windows and don't want to install Cygwin, then I suppose you would have to write a batch script to locate the jar files.

like image 45
Dave Costa Avatar answered Oct 05 '22 19:10

Dave Costa