如何确定应用程序中使用了哪些 JAR

发布于 2024-10-03 11:20:27 字数 140 浏览 0 评论 0原文

现有应用程序的类路径中有大量 JAR 文件。为了确定起见,最初必须有人添加所有 JAR。有些 JAR 显然没有被使用,我们已经删除了其中一些不需要的 JAR,没有造成任何问题。

如何确定哪些 JAR 正在使用,哪些 JAR 不需要(除了试错法之外)?

An existing application has a ton of JAR files in its classpath. Someone must have added all JARs initially just to be sure. Some of the JARs were obviously not being used and we've already removed some of these unneeded JARs without causing any problems.

How does one determine which JARs are being used and which ones are unneeded (besides a trial and error method)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

握住我的手 2024-10-10 11:20:27

Tattletale 是一个很棒的工具。它适用于字节码,因此某些类可能通过反射使用,并且不会出现在报告中。

此处链接不再有效)是示例报告。正如您所看到的,您只拥有您正在寻找的功能“Unused JAR”。

Tattletale is a great tool for this. It works on the bytecode, so it is possible, that some classes are use via reflection and will not come up in the report.

Here (link no longer works) is an example report. As you can see, you just have the feature you are looking for "Unused JAR".

雨夜星沙 2024-10-10 11:20:27

请注意,仅反复试验可能会出现问题,特别是当应用程序动态加载类(例如 Class.forName)时,因为删除 JAR 可能不会阻止应用程序启动并且(显然)正常工作,但如果找不到目标类,稍后可能会失败。

此外,还有许多工具可用于分析 Java 应用程序并找出依赖关系(我使用过 Dependency Finder 我自己,虽然不完全是为了这个目的),但请注意,他们中的大多数也将无法找到如上所述动态加载的类。

Be aware that trial and error alone can be a problem, especially if the application loads classes dynamically (e.g. Class.forName) as removing a JAR might not prevent the application from starting up and (apparently) work fine, but it may fail later if target classes are not found.

Also, there are many tools that can be used to analyze a Java application and find out dependencies (I have used Dependency Finder myself, although not exactly for this purpose), however note that most of them will also fail to find classes that are loaded dynamically as described above.

尘曦 2024-10-10 11:20:27

如果它们中的任何一个是动态加载的,则自动化工具可能会错过它们。我会重置文件的访问时间,运行应用程序一段时间(并确保调用尽可能多的功能),然后查看哪些文件被访问,哪些文件未被访问。您可能需要在应用程序需要运行的每个平台上重复此操作,以防万一。

If any of them are loaded dynamically, it's possible that automated tools will miss them. I would reset the access times on the files, run the application for some time (and make sure to invoke as much functionality as possible), and see which files were accessed and which were not. You may need to repeat this on each platform your application needs to run on, just in case.

喜你已久 2024-10-10 11:20:27

我在 jboss 门户项目中使用了以下 shell 脚本来获取 import 语句中使用的 jar 文件列表。这仅适用于直接依赖项,不适用于动态加载,甚至不适用于源中使用完全限定的类名。此外,所有 jar 文件及其传递依赖项均由容器提供,因此仅在编译代码时需要它们。

目标是为项目创建一个 Maven pom 并找到需要部署到我们的 Nexus 存储库管理器的文件。作为列出肯定需要的文件的起点可能很有用,其余的 jar 文件必须以其他方式检查。例如,如果该 jar 在 Maven 存储库中也可用,您可以查看其依赖项。

#!/bin/sh
JBOSS_HOME=/path/to/jboss/installation
JBOSS_LIB=$JBOSS_HOME/server/default/lib
JBOSS_DEPLOY=$JBOSS_HOME/server/default/deploy
SRC_DIR=src

for f in $JBOSS_LIB/*.jar $JBOSS_DEPLOY/jboss-portal.sar/lib/*.jar $JBOSS_DEPLOY/jboss-portal.sar/portal-cms.sar/lib/*.jar $JBOSS_DEPLOY/ejb3.deployer/*.jar
do
    for c in `jar -tf $f | tr '/
 '..'`
    do
        #echo "^import ${c%.class};"
        if `grep "^import ${c%.class};" -h -r $SRC_DIR -q`
        then
            echo $f $c
        fi
    done
done

I used the following shell script in a jboss portal project to get the list of jar files that are used in import statements. This will of yourse only work for direct dependencies, not for dynamically loaded or even when the fully qualified classname was used in the source. Furthermore, all jar files and their transitive dependencies are provided by the container so they are only needed to compile the code.

The goal was to create a maven pom for the project and to find the files that needed to be deployed to our nexus repository manager. It might be useful as a starting point to list the files that are definitely needed, the remaining jar files would have to be checked in other ways. If the jar is also available in a maven repository you might look at its dependencies for example.

#!/bin/sh
JBOSS_HOME=/path/to/jboss/installation
JBOSS_LIB=$JBOSS_HOME/server/default/lib
JBOSS_DEPLOY=$JBOSS_HOME/server/default/deploy
SRC_DIR=src

for f in $JBOSS_LIB/*.jar $JBOSS_DEPLOY/jboss-portal.sar/lib/*.jar $JBOSS_DEPLOY/jboss-portal.sar/portal-cms.sar/lib/*.jar $JBOSS_DEPLOY/ejb3.deployer/*.jar
do
    for c in `jar -tf $f | tr '/
 '..'`
    do
        #echo "^import ${c%.class};"
        if `grep "^import ${c%.class};" -h -r $SRC_DIR -q`
        then
            echo $f $c
        fi
    done
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文