如何获取主类的 Java 类依赖项列表?

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

How do I get a list of Java class dependencies for a main class?

javadependenciesdependency-management

提问by Ron Tuffin

Is there a way to find all the class dependencies of a java main class?

有没有办法找到java主类的所有类依赖关系?

I have been manually sifting through the imports of the main class and it's imports but then realized that, because one does not have to import classes that are in the same package, I was putting together an incomplete list.

我一直在手动筛选主类的导入和它的导入,但后来意识到,因为不必导入同一包中的类,所以我整理了一个不完整的列表。

I need something that will find dependencies recursively (perhaps to a defined depth).

我需要一些可以递归查找依赖项的东西(可能到定义的深度)。

采纳答案by Tom Hawtin - tackline

It can be eaily done with just javac. Delete your class files and then compile the main class. javac will recursively compile any classes it needs (providing you don't hide package private classes/interfaces in strangely named files). Of course, this doesn't cover any recursive hacks.

只需使用 javac 即可轻松完成。删除您的类文件,然后编译主类。javac 将递归编译它需要的任何类(前提是您不要在奇怪命名的文件中隐藏包私有类/接口)。当然,这不包括任何递归黑客。

回答by Ron Tuffin

I just found Class Dependency Analyzer, download, install configure and 10 min's later I have a complete class Dependency list.

我刚刚找到Class Dependency Analyzer,下载,安装配置,10 分钟后我有一个完整的类依赖列表。

回答by Daniel Alencar

You may want to take a look at the jdepscommand line tool:

您可能想看看jdeps命令行工具:

https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jdeps.html

https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jdeps.html

jdeps -cp <your cp> -v <path to your .class file>

The command will return a list of the dependencies for that java class file. There are many other options that you can use in your command like -regexto find dependencies matching a given pattern

该命令将返回该 java 类文件的依赖项列表。您可以在命令中使用许多其他选项,例如-regex查找与给定模式匹配的依赖项

回答by Andreas Petersson

in intellij ideayou have the nifty dependency structure matrix. you can explore package- and class-level dependencies perfectly. if you like it simpler, you have a dependency viewer as well, but htere you can only see first-order dependencies.

Intellij 想法中,您拥有漂亮的依赖结构矩阵。您可以完美地探索包级和类级的依赖关系。如果你喜欢更简单的,你也有一个依赖查看器,但是你只能看到一阶依赖。

回答by Andrushenko Alexander

Here is a code snippet you can put into your main method in Java to output paths to all dependencies of your application:

这是一个代码片段,您可以将其放入 Java 的 main 方法中,以输出应用程序所有依赖项的路径:

ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader) cl).getURLs();
for(URL url: urls){
    System.out.println(url.getPath());
}