Java - 如何找出导入来自哪个 Jar 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3609643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - How to find out which Jar file an import comes from?
提问by Greg
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:
我正在处理的项目有大约 10 个 jar 文件作为库。在其中一个文件的顶部有一个导入语句,如:
import jpe.nar.crat.maker.ObjectMakerFactory;
Is there a way to tell which Jar file it comes from?
有没有办法判断它来自哪个 Jar 文件?
(I'm using Netbeans if that matters.)
(如果重要的话,我正在使用 Netbeans。)
采纳答案by BalusC
You can use CodeSource#getLocation()for this. The CodeSourceis available by ProtectionDomain#getCodeSource(). The ProtectionDomainin turn is available by Class#getProtectionDomain().
您可以CodeSource#getLocation()为此使用。由CodeSource提供ProtectionDomain#getCodeSource()。该ProtectionDomain又是通过提供Class#getProtectionDomain()。
URL location = ObjectMakerFactory.class.getProtectionDomain().getCodeSource().getLocation();
System.out.println(location.getPath());
// ...
回答by Roy
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.
你有没有试过在课堂上做一个“开放声明”?在 Eclipse 中,当您执行此操作时,它会打开一个窗口,显示 jar 的名称并告诉您此 jar 具有“无源附件”。我希望 NetBeans 也能发生类似的事情。
Thanks, R
谢谢,R
回答by Jan
I like JFindvery much:
我非常喜欢JFind:
... it works recursively by looking into jar's, inside war's, inside ear's, etc...
...它通过查看罐子、War内部、耳朵内部等递归地工作......
If you wrap the java launcher in a shell script and put that on your PATH, it becomes a very powerful tool:
如果您将 java 启动器包装在一个 shell 脚本中并将其放在您的 PATH 中,它就会成为一个非常强大的工具:
I.e. to find all EntityManagerclasses in directory jboss-6.0.0.20100429-M3:
即EntityManager在目录中查找所有类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:
用于 Cygwin 的小外壳包装器:
if [ $# -ne 2 ]
then
echo "Usage: `basename ##代码##` <classname> [<fromDir>]"
exit 1
fi
echo Search String:
SEARCH_LOCATION=`cygpath -w `
echo Windows Search Location: $SEARCH_LOCATION
java -jar `cygpath -w $HOME/bin/JFind.jar` "" "$SEARCH_LOCATION"
echo
回答by wolfman
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.
在 Netbeans IDE 中:在您的代码中,按住 ctrl 并选择类名。在编辑器中出现相关的 java 文件后(如果您没有该类的源代码,它可能不会显示太多)。右键单击源窗口中的任意位置,然后选择“在项目中选择”。该类将在它来自的 jar 中突出显示。
This has been working at least as far back as Netbeans 8.0.
这至少可以追溯到 Netbeans 8.0。
回答by Colin Hebert
You can use the Jar Class Searchfor netbeans. I'm not sure that it still compatible, but it's worth the try.
您可以对 netbeans使用Jar 类搜索。我不确定它是否仍然兼容,但值得一试。
回答by Michael Lloyd Lee mlk
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.
你可以试试DocJar。在 Eclipse 控件中单击该项目将显示您(编辑面板将显示源(如果附加)或可用的方法,而包资源管理器将打开该类的树),如果 Netbeans 没有以类似的方式运行,我会感到惊讶庄园。
回答by OliBlogger
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.
我的前同事 Tom 编写了 JarSniffer ( http://sourceforge.net/projects/jarsniffer/)。这是一个方便的小工具,可以在一组 jars、zip 和目录树中找到一个类。

