java 如何分析Java程序中使用了哪个jar文件?

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

How to analyse which jar file is used in a Java program?

javaclasspath

提问by omg

Suppose there is a jar file named callme.jar

假设有一个名为 callme.jar 的 jar 文件

and it's located in several directories,

它位于几个目录中,

how to analyse which one of them is used at run-time?

如何分析在运行时使用了其中的哪一个?

回答by matt b

Invoke the javaexecutable with the -verbose:classargument. This will produce output like:

java使用-verbose:class参数调用可执行文件。这将产生如下输出:

[Loaded org.apache.log4j.helpers.ThreadLocalMap from file:/C:/.../1.2.14/log4j-1.2.14.jar]
[Loaded org.apache.commons.cli.Option from file:/C:/.../commons-cli-1.2.jar]

[从文件加载 org.apache.log4j.helpers.ThreadLocalMap:/C:/.../1.2.14/log4j-1.2.14.jar]
[从文件加载 org.apache.commons.cli.Option:/C :/.../commons-cli-1.2.jar]

回答by Herms

Might not be the easiest method, but you could look at what files the process has open and determine it from that.

可能不是最简单的方法,但您可以查看进程打开了哪些文件并从中确定。

If you're on windows you could use Process Explorerto see what files the process has open at any given time, or Process Monitorto watch the filesystem access as it runs. There will be a lot of noise, but you could figure it out from there.

如果您在 Windows 上,您可以使用Process Explorer查看进程在任何给定时间打开了哪些文件,或使用Process Monitor在运行时监视文件系统访问。会有很多噪音,但你可以从那里弄清楚。

If you're on the Mac I think the built-in Activity Monitor can give you a list of open files. Sadly I don't know the command you'd use in Linux.

如果您使用的是 Mac,我认为内置的活动监视器可以为您提供打开文件的列表。遗憾的是,我不知道您在 Linux 中使用的命令。

回答by rich

System.getProperty("java.class.path");

回答by g andrieu

Try this piece of code :

试试这段代码:

    //Get the System Classloader
    ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();

    //Get the URLs
    URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
    System.out.println("CURRENT CLASSPATH :");
    for(int i=0; i< urls.length; i++){
        System.out.println(urls[i].getFile());
    }       
    System.out.println("END OF CLASSPATH");

回答by Avinash Kadam

It is not that easy, to know whether piece of code from JAR is being used is only possible when your code calling that jar is being executed (i.e. being covered). Till the point your code isn't covered it is difficult to know whether jar on your application class path is being used or not.

并不是那么容易,只有在调用该 jar 的代码正在执行(即被覆盖)时才能知道是否正在使用 JAR 中的一段代码。直到你的代码没有被覆盖,很难知道你的应用程序类路径上的 jar 是否正在被使用。

For this you might need to right good junit test cases which can cover you entire code and then you can use Class Dependency Analyzer tools.

为此,您可能需要正确的 junit 测试用例,它可以覆盖您的整个代码,然后您可以使用类依赖分析器工具。

It is kind of try and see thing. You can try removing JARs one by one and check whether application do works or not but junit cases are the best options to do this examination.

这是一种尝试和观察的东西。您可以尝试一个一个删除 JAR 并检查应用程序是否有效,但是 junit 案例是进行此检查的最佳选择。