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
.
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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With