Java 找到给定类名的 jar 文件?

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

Find a jar file given the class name?

javajardependencies

提问by Jeffrey Knight

This must be a very basic question for Java developers, but what is the best way to find the appropriate jar filegiven a class name?

对于 Java 开发人员来说,这一定是一个非常基本的问题,但是在给定类名的情况下,找到合适的jar 文件的最佳方法是什么?

For example, given "com.ibm.websphere.security.auth.WSSubject", how do you track down the appropriate jar file? ("google" is not the answer I'm looking for!)

例如,给定“ com.ibm.websphere.security.auth.WSSubject”,您如何追踪相应的 jar 文件?(“google”不是我要找的答案!)

The java docsdo not give any hint of the jar file, and obviously the names of the jar files themselves offer no clue.

Java文档不给jar文件的任何暗示,明显的jar文件的名称本身不提供线索。

There must be a 'search local jars', or some sort of 'auto-resolve dependencies', trick in the java world. Ideally, I'm looking for the 'official' way to do this. I happen to be on a windows machine without cygwin.

在 Java 世界中必须有“搜索本地 jars”或某种“自动解析依赖项”的技巧。理想情况下,我正在寻找“官方”的方式来做到这一点。我碰巧在没有 cygwin 的 Windows 机器上。

采纳答案by Dan Dyer

Save this as findclass.sh (or whatever), put it on your path and make it executable:

将其另存为 findclass.sh(或其他),将其放在您的路径上并使其可执行:

#!/bin/sh
find "" -name "*.jar" -exec sh -c 'jar -tf {}|grep -H --label {} ''' \;

The first parameter is the directory to search recursively and the second parameter is a regular expression (typically just a simple class name) to search for.

第一个参数是要递归搜索的目录,第二个参数是要搜索的正则表达式(通常只是一个简单的类名)。

$ findclass.sh . WSSubject

The script relies on the -t option to the jar command (which lists the contents) and greps each table of contents, labelling any matches with the path of the JAR file in which it was found.

该脚本依赖于 jar 命令的 -t 选项(它列出内容)并 grep 每个目录,用找到它的 JAR 文件的路径标记任何匹配项。

回答by Pascal Thivent

You could try services like:

您可以尝试以下服务:

Or

或者

Or

或者

  • A maven enterprise repository with a search feature e.g. Nexus (OFC, this would only work if the jars you're looking for are indexed i.e. installed in the repository)
  • 具有搜索功能的 Maven 企业存储库,例如 Nexus(OFC,这仅在您要查找的 jar 已编入索引即安装在存储库中时才有效)

PS: Jarhoo has teamed up with Javacio.us to provide 100,000 Java developers with free access to Jarhoo via links integrated with their Google search results. Subscription to Javacio.us is free and open to anyone with a Google account. For more information, please visit the Jarhoo offer page at Javacio.us.

PS:Jarhoo 已与 Javacio.us 合作,通过与 Google 搜索结果集成的链接,为 100,000 名 Java 开发人员提供免费访问 Jarhoo 的权限。订阅 Javacio.us 是免费的,并且对任何拥有 Google 帐户的人开放。有关更多信息,请访问Javacio.us 上Jarhoo 报价页面

回答by Paul Tomblin

locate .jar | xargs grep com.ibm.websphere.security.auth.WSSubject

找到.jar | xargs grep com.ibm.websphere.security.auth.WSSubject

回答by Dave Costa

There is no "official" Java way to do this AFAIK.

没有“官方”Java 方法来执行此 AFAIK。

The way I usually hunt for it is to use findand jarto look through all jar files in a given tree.

我通常寻找它的方法是使用findjar来查看给定树中的所有 jar 文件。

> find . -name \*.jar -print -exec jar tf {} oracle/sql/BLOB.class \;
./v2.6.1/lib/csw_library.jar
./v2.6.1/lib/oracle_drivers_12_01.jar
oracle/sql/BLOB.class

If you're on Windows and don't want to install Cygwin, then I suppose you would have to write a batch script to locate the jar files.

如果您使用的是 Windows 并且不想安装 Cygwin,那么我想您必须编写一个批处理脚本来定位 jar 文件。

回答by Steve B.

Printing the list as I go so I can see what I'm checking. Mostly I'm looking in a lib/app directory, but you can substitute a locate for the find.

边走边打印列表,这样我就可以看到我在检查什么。我主要是在 lib/app 目录中查找,但您可以用 locate 替换 find。

e.g.

例如

for jar in $(find some_dir/lib -name "*.jar" ); 
do
echo -------------$jar-------------------
jar -tf $jar | grep TheNameOfTheClassImLookingFor
done

回答by aperkins

Given your comment on attempting to handle dependencies, what I would do is focus on which libraries you are using. Knowing this, you will know what jars have to be on the classpath, and you can pay attention to that. There are also dependency management builders (Maven and Ant being two popular ones) that can package up projects with their dependencies inside. However, in the end, it is up to the developer to know which dependencies they have, and to pay attention to the requirements for those dependencies. This is one reason why using an IDE like Eclipse, and build tools like Maven or Ant are so nice in large projects, as when you have 20 dependencies for a project, that can become pretty unmanageable.

鉴于您对尝试处理依赖项的评论,我要做的就是关注您正在使用哪些库。知道了这一点,您就会知道类路径上必须有哪些 jar,并且您可以注意这一点。还有依赖管理构建器(Maven 和 Ant 是两个流行的构建器)可以将项目及其依赖项打包在一起。但是,最终还是由开发人员知道他们有哪些依赖项,并关注这些依赖项的要求。这就是为什么在大型项目中使用像 Eclipse 这样的 IDE 和像 Maven 或 Ant 这样的构建工具如此出色的原因之一,因为当一个项目有 20 个依赖项时,这会变得非常难以管理。

回答by cetnar

I recommend using Maven, Eclipse and m2eclipse.

我推荐使用 Maven、Eclipse 和 m2eclipse。

Step 1 - add specific import

第 1 步 - 添加特定导入

alt text

替代文字

Step 2 - find and download (automatically) desired jar

第 2 步 - 查找并下载(自动)所需的 jar

alt text

替代文字

回答by Yishai

In Windows, run cmd.exe and type:

在 Windows 中,运行 cmd.exe 并键入:

  for %i in (*.jar) do @jar tvf %i | find "/com/company/MyClass.class"

The jars would have to be in the current directory. For also has a /R option which takes a directory and lets you search recursively.

jar 必须在当前目录中。For 还有一个 /R 选项,它接受一个目录并让您递归搜索。

If Jar.exe isn't in your path, you can do something like @C:\jdk\bin\jar.exe.

如果 Jar.exe 不在您的路径中,您可以执行类似 @C:\jdk\bin\jar.exe 的操作。

回答by Stephen Muench

If the grep on your system (e.g. Solaris) doesn't have -H and --label as used in Dan Dyer's example, you can use:

如果您的系统(例如 Solaris)上的 grep 没有 Dan Dyer 示例中使用的 -H 和 --label,您可以使用:

find . -name '*.jar' -type f | xargs -i bash -c "jar -tvf {}| tr / . | grep WSSubject && echo {}"

回答by Joseph

Try findjar.com. If it's in the public domain, you should get it. There's alos mavenjava.com (but that site seems to be down)

试试 findjar.com。如果它在公共领域,你应该得到它。有 alos mavenjava.com(但该站点似乎已关闭)