How do I get a list of Java class dependencies for

2019-02-12 19:36发布

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

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

5条回答
Fickle 薄情
2楼-- · 2019-02-12 20:20

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.

查看更多
做自己的国王
3楼-- · 2019-02-12 20:22

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

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 -regex to find dependencies matching a given pattern

查看更多
Anthone
4楼-- · 2019-02-12 20:29

in intellij idea you 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.

查看更多
虎瘦雄心在
5楼-- · 2019-02-12 20:33

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

查看更多
够拽才男人
6楼-- · 2019-02-12 20:33

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

ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader) cl).getURLs();
for(URL url: urls){
    System.out.println(url.getPath());
}
查看更多
登录 后发表回答