Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print imported java libraries?

Tags:

java

Is there a way to print within a Java Code the libraries that has been imported, and available during the execution?

For example :

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.

like image 654
alain.janinm Avatar asked Jan 18 '23 16:01

alain.janinm


2 Answers

If you need the actual imports used in your source code (rather than using the information in the bytecode), you can use a library called QDox which will parse your source code and can get a list of the imports you use:

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

The output is:

com.thoughtworks.qdox.JavaDocBuilder
javax.swing.JFrame
like image 140
Martin Ellis Avatar answered Jan 20 '23 04:01

Martin Ellis


I don't think that there is a way to do that. Imports are only a syntactic aid for the programmer and are not reflected in the compiled class files. Anyway, what do you need such a feature for?

like image 32
Natix Avatar answered Jan 20 '23 04:01

Natix