简体   繁体   English

Java:如何根据类名知道要使用哪个jar文件?

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

Given a class, org.eclipse.ui.views.navigator.ResourceNavigator for example, how do I find out which jar file to use? 给定一个类,例如org.eclipse.ui.views.navigator.ResourceNavigator ,如何找出要使用的jar文件? I know it's in org.eclipse.ui.ide, but how would I find that out? 我知道它在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. 我希望javadoc包含这些信息。 So far here are the different methods: 到目前为止,这里有不同的方法:

  1. Without Internet, Eclipse or NetBeans: 没有Internet,Eclipse或NetBeans:

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

  3. If you want to find out from Internet or you do not have the jar yet: 如果你想从互联网上找到你或者你还没有jar:

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(但可能有点过时)

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). 用户输入未找到的类的名称(或Java项目需要访问的类的名称)。 The plug-in will search the selected directory (and subdirectories) for JAR files containing that class. 该插件将在所选目录(和子目录)中搜索包含该类的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. 然后,用户可以浏览此表并选择要添加到其Java项目构建路径的JAR文件。 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. 然后,用户右键单击表中的条目,然后从上下文菜单中选择要添加它的构建路径。


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 sunleo comments below : 正如sunleo评论如下

with Eclipse, Ctfl + Shift + T remains the easiest alternative to look for a type 使用Eclipse, Ctfl + Shift + T仍然是查找类型的最简单方法
(with the jar name displayed in the status bar). (jar状态名称显示在状态栏中)。


user862268 comments below : user862268评论如下

For mac, it is cmd + shift + T in Eclipse to find the class and associated jar. 对于mac,在Eclipse中是cmd + shift + T来查找类和关联的jar。

go for Ctrl+Shift+T in Eclipse IDE 在Eclipse IDE中转到Ctrl + Shift + T.

Open Type dialog appears where you enter the class name. 出现打开类型对话框,您可以在其中输入类名。 after that package identifier and jar destination get displayed. 之后显示包标识符和jar目标。

To answer the question, there is no real way to know which jar to use. 要回答这个问题,没有真正的方法可以知道使用哪个罐子。 Different versions will have potentially different behaviour. 不同的版本可能会有不同的行为。

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 $1; 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搜索

For me this works fine: 对我来说这很好:

find * -type f -name '*.jar' -exec grep -l 'TestClass.class' '{}' \;

Regards 问候

Use this: netbeans plugin 使用此: netbeans插件

Or jarFinder service jarFinder服务

**shell> find . -name "*.jar" | xargs -i -t \jar tvf {} | grep [TheClassNameYouAreLookingFor]**

For example, if you are looking for a class LogFactory.class on list of apache commons jars, below command would work 例如,如果您在apache commons jar列表中查找类LogFactory.class,则下面的命令将起作用

shell> find apache-commons/ -name "*.jar" | xargs -i -t \jar tvf {} | grep LogFactory.class

jar tvf apache-commons/commons-beanutils-1.7.0.jar

jar tvf apache-commons/commons-fileupload-1.2.1.jar

jar tvf apache-commons/commons-lang-2.3.jar

**jar tvf apache-commons/commons-logging-1.1.jar**

**21140 Tue May 09 23:08:12 EDT 2006 org/apache/commons/logging/LogFactory.class**

jar tvf apache-commons/commons-net-1.3.0.jar

The above result indicates that the match is commons-logging-1.1.jar 上面的结果表明匹配是commons-logging-1.1.jar

Use a class/JAR locator: 使用class / 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插件。

I use FindJar.com . 我使用FindJar.com It lists all known packages that contain any given class! 它列出了包含任何给定类的所有已知包! It's incredibly helpful. 这非常有帮助。

Usually, I do jar -vtf foo.jar to get a list of all the class files. 通常,我做jar -vtf foo.jar来获取所有类文件的列表。
Not the most practical way, but handy. 不是最实用的方式,但方便。 You can combine the result with grep, of course. 当然,您可以将结果与grep结合起来。

在Intellij IDEA中,只需按住Ctrl键单击类名,就可以转到该类的伪源代码,窗口的标题将类似于c:\\ path \\ to \\ lib.jar!\\ com \\ something \\ ClassName。类

I vote for CTRL-SHIFT-T because it is already included in Eclipse. 我投票给CTRL-SHIFT-T,因为它已经包含在Eclipse中。 I actually dropped jarclassfinder.jar in the plugins dir within the eclipse installation but still couldn't find the plugin in Eclipse GUI 我实际上在eclipse安装中的插件目录中删除了jarclassfinder.jar,但仍然无法在Eclipse GUI中找到该插件

您也可以尝试Jarvana来查找特定类的jar文件。

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 ZIP files. JAR(Java ARchive)文件只不过是ZIP文件。 All you have to do is unzip the jar, or even view it using a zip-reading application. 您所要做的就是解压缩jar,甚至使用zip阅读应用程序查看它。 Under Windows XP, for example, this comes built into the operating system. 例如,在Windows XP中,它内置于操作系统中。 How convenient. 多方便

Hope this helped... 希望这有助于......

Yuval =8-) Yuval = 8-)

另请查看http://javacio.us/

if you tell eclipse to open a declaration (F3) or implementation (Navigate->Open Implementation) and you get popped into an un-editable edit window, you can see the path to the jar file by right-clicking the editor window and choosing 'show in breadcrumbs' 如果你告诉eclipse打开一个声明(F3)或实现(Navigate-> Open Implementation)并且你进入一个不可编辑的编辑窗口,你可以通过右键单击编辑器窗口并选择它来查看jar文件的路径'用面包屑显示'

my find unix based: 我的发现基于unix:

find . -name "*.jar" -print -exec jar -tf {} \; >outputfile

edit output file to find what you want (instead of grep) 编辑输出文件以找到你想要的(而不是grep)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道给定的 jar 文件使用了哪些 class / package/ 方法? - how to know which class / package/ methods used by given jar file? 我如何使用jar文件中的Liquibase更改日志 - How do I use a Liquibase changelog which is in jar file 如何知道被检测的类的jar名称 - how to know the jar name of the class being instrumented java 在mavenLocal()中为.jar文件成功添加gradle依赖关系之后,我实际上如何从Java类导入和使用该jar - After successfully adding a gradle dependency for a .jar file in mavenLocal(), how do I actually import and use that jar from a java class 我如何知道Java程序需要运行哪些jar文件? - How do I know which jar files java program needs to run? Java - 如何使用类文件? - Java - How do I use a class file? 在给定 class 名称的情况下查找 jar 文件? - Find a jar file given the class name? 如何导入 .jar 并使用它的类,以及如何从 netbeans 中的 .jar 文件使用 stmt? - How do I import .jar and use its class and also how do i use stmt from a .jar file in netbeans? 在Eclipse中测试Java GUI时,我怎么知道调用哪个方法/类? - How do I know which method/class is called when I test java GUI in eclipse? 我怎么知道哪个maven包中包含我要使用的类? - How do I know which maven package contains the class I am going to use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM