Java:我如何知道给定类名要使用哪个 jar 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/275120/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:22:34  来源:igfitidea点击:

Java: How do I know which jar file to use given a class name?

javaeclipsejar

提问by Eugene Yokota

Given a class, org.eclipse.ui.views.navigator.ResourceNavigatorfor example, how do I find out which jar file to use? I know it's in org.eclipse.ui.ide, but how would I find that out?

给定一个类,例如org.eclipse.ui.views.navigator.ResourceNavigator,我如何找出要使用的 jar 文件?我知道它在 org.eclipse.ui.ide 中,但我如何找到它?

Edit: Thank you to all of you who answered. Like many things, there seems to be several ways to skin this cat. I wish javadoc contained this info. So far here are the different methods:

编辑:感谢所有回答的人。像许多事情一样,似乎有几种方法可以给这只猫剥皮。我希望 javadoc 包含此信息。到目前为止,这里是不同的方法:

  1. Without Internet, Eclipse or NetBeans:

    for f in `find . -name '*.jar'`;  do echo $f && jar tvf $f | grep -i ; done
    
  2. If you want to find out locally using Eclipse:

  3. If you want to find out from Internet or you do not have the jar yet:

  1. 没有 Internet、Eclipse 或 NetBeans:

    for f in `find . -name '*.jar'`;  do echo $f && jar tvf $f | grep -i ; done
    
  2. 如果您想使用 Eclipse 在本地查找:

  3. 如果您想从互联网上查找或者您还没有 jar:

采纳答案by VonC

You also have this eclipse plugin: jarclassfinder

你还有这个 eclipse 插件:jarclassfinder

The user enters the name of the class not found (or the name of the class that the Java project needs to access). The plug-in will search the selected directory (and subdirectories) for JAR files containing that class.

用户输入未找到的类名(或Java项目需要访问的类名)。该插件将在选定的目录(和子目录)中搜索包含该类的 JAR 文件。

All results are displayed in a table in a custom view. The user can then browse this table and select the JAR file to add to his Java project's build path. The user then right-clicks on the entry in the table and, from the context menu, selects the build path to which to add it.

所有结果都显示在自定义视图的表格中。然后,用户可以浏览此表并选择 JAR 文件以添加到他的 Java 项目的构建路径中。然后,用户右键单击表中的条目,并从上下文菜单中选择要将其添加到的构建路径。



Update 2013, as I mention in "searching through .jar files eclipse", it is no longer maintained, and the alternatives are sparse.

更新 2013,正如我在“搜索 .jar 文件 eclipse”中提到的,它不再维护,替代品很少。

As sunleocomments below:

正如sunleo在下面评论的那样

with Eclipse, Ctfl+Shift+Tremains the easiest alternativeto look for a type
(with the jar name displayed in the status bar).

与Eclipse,Ctfl+ Shift+T仍然是最简单的替代寻找一个类型
(有罐子名在状态栏中显示)。



user862268comments below:

user862268评论如下

For mac, it is cmd+shift+Tin Eclipse to find the class and associated jar.

对于Mac,这是cmd+ shift+T在Eclipse中找到类和相关的罐子。

回答by PhiLho

Usually, I do jar -vtf foo.jarto get a list of all the class files.
Not the most practical way, but handy. You can combine the result with grep, of course.

通常,我会jar -vtf foo.jar获取所有类文件的列表。
不是最实用的方法,但很方便。当然,您可以将结果与 grep 结合使用。

回答by Yuval Adam

Use a class/JAR locator:

使用类/JAR 定位器:

http://classlocator.sourceforge.net/

http://classlocator.sourceforge.net/

[EDIT] It isn't obvious, even from ClassLocator's docs (!) but it seems to be an Eclipse plugin.

[编辑] 即使从 ClassLocator 的文档 (!) 中也不明显,但它似乎是一个 Eclipse 插件。

回答by pkliem

If you have the jar in your class path / project path hit CTRL-SHIFT-T and type the name ... the jar will be displayed at the bottom.

如果您的类路径/项目路径中有 jar,请按 CTRL-SHIFT-T 并键入名称……该 jar 将显示在底部。

If you haven't the class in your build path a) put together a dummy project containing all the jars b) I think there is a plugin to find jars from IBM Alphaworks (but that might be kind of outdated)

如果您的构建路径中没有该类 a) 将一个包含所有 jar 的虚拟项目放在一起 b) 我认为有一个插件可以从 IBM Alphaworks 中查找 jar(但这可能有点过时了)

回答by iberck

Use this: netbeans plugin

使用这个:netbeans 插件

Or jarFinderservice

或者jarFinder服务

回答by user28791

To answer the question, there is no real way to know which jar to use. Different versions will have potentially different behaviour.

要回答这个问题,没有真正的方法知道要使用哪个 jar。不同的版本可能会有不同的行为。

When it comes to locating a jar which contains a given class, I use:

在定位包含给定类的 jar 时,我使用:

for f in `find . -name '*.jar'`;  do echo $f && jar tvf $f | grep -i ; done

This will highlight any jar containing the classname passed in as a parameter in any subfolder.

这将突出显示包含作为任何子文件夹中的参数传入的类名的任何 jar。

Another good way to find a class is to use the maven repos search.

另一个查找类的好方法是使用maven repos search

回答by Yuval

I'm not sure I really understand the question, but if you're looking to verify that your class is really in the jar, you can always look through the jar itself, and for that you don't need any Eclipse plugins or specialized external application.

我不确定我是否真的理解这个问题,但是如果您想验证您的类是否真的在 jar 中,您可以随时查看 jar 本身,为此您不需要任何 Eclipse 插件或专门的外用。

JAR (Java ARchive) files are nothing more than ZIPfiles. All you have to do is unzip the jar, or even view it using a zip-reading application. Under Windows XP, for example, this comes built into the operating system. How convenient.

JAR (Java ARchive) 文件只不过是ZIP文件。您所要做的就是解压缩 jar,甚至使用 zip-reading 应用程序查看它。例如,在 Windows XP 下,它内置于操作系统中。多么方便。

Hope this helped...

希望这有助于...

Yuval =8-)

尤瓦尔 =8-)

回答by Kieveli

I use FindJar.com. It lists all known packages that contain any given class! It's incredibly helpful.

我使用FindJar.com。它列出了包含任何给定类的所有已知包!这非常有帮助。

回答by Pavel Feldman

In Intellij IDEA you just ctrl-click on class name and you are will be moved to pseudo source code of that class, and title of window will be like c:\path\to\lib.jar!\com\something\ClassName.class

在 Intellij IDEA 中,您只需按住 ctrl 单击类名,您将被移动到该类的伪源代码,窗口标题将类似于 c:\path\to\lib.jar!\com\something\ClassName。班级

回答by Adisesha

Also check http://javacio.us/.

还要检查http://javacio.us/