How to print imported java libraries?

Is there a way to print libraries that were imported and available at runtime in Java code?

For instance:

import javax.swing.JFrame; public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { //some code } } 

I need to print javax.swing.JFrame .

+4
source share
2 answers

If you need the actual import used in the source code (instead of using the information in the bytecode), you can use the QDox library, which will analyze the source code and get a list of the import it uses:

Main.java

 import com.thoughtworks.qdox.JavaDocBuilder; import javax.swing.JFrame; public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { JavaDocBuilder java = new JavaDocBuilder(); java.addSourceTree(new java.io.File(".")); for (String i : java.getClassByName("Main").getSource().getImports()) { System.out.println(i); } } } 

Compile and run with:

 # If you don't have wget, just download the QDox jar by hand wget -U "" http://repo1.maven.org/maven2/com/thoughtworks/qdox/qdox/1.12/qdox-1.12.jar javac -classpath qdox-1.12.jar Main.java java -classpath qdox-1.12.jar:. Main 

Output:

 com.thoughtworks.qdox.JavaDocBuilder javax.swing.JFrame 
+7
source

I don’t think there is a way to do this. Import is just the syntactic help of a programmer and is not reflected in the files of compiled classes. Anyway, why do you need such a function?

+2
source

Source: https://habr.com/ru/post/1393146/


All Articles