Jason Thompson
Jason Thompson

Reputation: 4833

Given a directory, how do I find out where a class is

Consider that I have a directory full of jars. Is there a java command that I can type into the command prompt that will tell me which jars a class can be loaded from?

Upvotes: 1

Views: 92

Answers (5)

Jose Luis Martin
Jose Luis Martin

Reputation: 10709

May be something like this:

findclass.sh

#!/bin/sh

if [ 2 != $# ] ; then
     echo  "Usage: $0 directory class"
     exit 1
fi

for i in $1/*.jar; do
    if unzip -l $i | grep -q "$2.class"; then 
        echo "Found $2 in $i"
   fi
done

Upvotes: 0

mgaert
mgaert

Reputation: 2388

If you want to know when and where classes are loaded into the JVM at runtime, you can use the -verbose:class option on the "java" command.

Upvotes: 0

mgaert
mgaert

Reputation: 2388

Have a look at "Swiss File Knife" on Sourceforge (http://sourceforge.net/projects/swissfileknife/). To search for some class named, say, QueryParameter, use

>sfk larc -dir "*.jar" +filter -+QueryParameter
gwt\gwt-dev.jar\\org\apache\xalan\lib\sql\QueryParameter.class

The JAR file is mentioned before the double backslash, the path within the JAR is after that.

Once you get used to the compact syntax of SFK, you'll use it for many, many things. Excellent companion to grep, find, and the like.

Upvotes: 0

Mike K.
Mike K.

Reputation: 3789

There's an app for this, FindClass that can provide clean output. They have source as well, they may just be grepping through the jars.

Upvotes: 1

Bombe
Bombe

Reputation: 83847

“Java commands” (whatever they may be) are usually not entered on the command line. You might try a simple grep, though:

 # grep 'my.super.cool.Stuff' *.jar

Theoretically the dots need to be escaped to prevent them from matching any character but this will generally give you an idea where a class is located.

Upvotes: 2

Related Questions