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 { 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:
Output:
com.thoughtworks.qdox.JavaDocBuilder javax.swing.JFrame
source share