找到一个给定类名的jar文件?

2022-01-15 00:00:00 dependencies java jar

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

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

例如,给定com.ibm.websphere.security.auth.WSSubject",你如何追踪合适的jar文件?(google"不是我要找的答案!)

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!)

java docs 没有给出 jar 文件的任何提示,显然 jar 文件的名称本身没有提供线索.

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

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

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.

推荐答案

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

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

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

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

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

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

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.

相关文章