tags:

views:

184

answers:

6

The project I'm working on has about 10 jar files as libraries. At the top of one of the files there's an import statement like:

import jpe.nar.crat.maker.ObjectMakerFactory;

Is there a way to tell which Jar file it comes from?

(I'm using Netbeans if that matters.)

+7  A: 

You can use CodeSource#getLocation() for this. The CodeSource is available by ProtectionDomain#getCodeSource(). The ProtectionDomain in turn is available by Class#getProtectionDomain().

URL location = ObjectMakerFactory.class.getProtectionDomain().getCodeSource().getLocation();
System.out.println(location.getPath());
// ...
BalusC
+2  A: 

You can use the Jar Class Search for netbeans. I'm not sure that it still compatible, but it's worth the try.

Colin Hebert
+1  A: 

Programmaticly or interactively?

You can try DocJar. In Eclipse control click on the item will show you (the edit panel will show the source (if attached) or the methods available while Package Explorer will open the tree to the class), I would be surprised if Netbeans did not behave in a similar manor.

mlk
+1  A: 

I like JFind very much:

http://jfind.sourceforge.net/

... it works recursively by looking into jar's, inside war's, inside ear's, etc...

If you wrap the java launcher in a shell script and put that on your PATH, it becomes a very powerful tool:

I.e. to find all EntityManager classes in directory jboss-6.0.0.20100429-M3 :

$ jfind.sh EntityManager ./jboss-6.0.0.20100429-M3
Search String: EntityManager
Windows Search Location: jboss-6.0.0.20100429-M3

....jjj.jjjjjjjjjjj
ClassName = javax/persistence/EntityManager.class
JarName = jboss-6.0.0.20100429-M3\client\hibernate-jpa-2.0-api.jar
----------------


jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
ClassName = org/apache/xerces/impl/XMLEntityManager.class
JarName = jboss-6.0.0.20100429-M3\client\xercesImpl.jar
----------------

A little shell wrapper for use in Cygwin:

if [ $# -ne 2 ]
then
  echo "Usage: `basename $0` <classname> [<fromDir>]"
  exit 1
fi

echo Search String: $1
SEARCH_LOCATION=`cygpath -w $2`
echo Windows Search Location: $SEARCH_LOCATION

java -jar `cygpath -w $HOME/bin/JFind.jar` "$1" "$SEARCH_LOCATION"
echo
Jan
A: 

Have you tried doing a 'Open Declaration' on the class? In Eclipse, when you do it, it opens a window that shows the name of the jar and tells you that this jar has 'No Source Attachment'. I am hoping something similar should happen for NetBeans.

Thanks, R

Roy
A: 

A former colleague of mine, Tom, wrote JarSniffer (http://sourceforge.net/projects/jarsniffer/). It's a handy little tool to find a class in a set of jars, zips and directory trees.

OliBlogger