开发者

Java - How to find out which Jar file an import comes from?

开发者 https://www.devze.com 2023-01-14 19:05 出处:网络
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:

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 开发者_JS百科Jar file it comes from?

(I'm using Netbeans if that matters.)


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());
// ...


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


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


In the Netbeans IDE: In your code, ctrl-select the class name. After the relevant java file comes up in the editor (if you don't have source for the class, it might not show much). Right-click anywhere in the source window and select "Select In Projects". The class will show highlighted in the jar where it came from.

This has been working at least as far back as Netbeans 8.0.


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


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.


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.


In Eclipse just do Ctrl-Click on the class name (in import sentence), then Right- Click on .class code, to open a dialog,·and finally chose Declarations-project.

Java - How to find out which Jar file an import comes from?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号