4

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.

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • Don't think it's possible since it's just syntactic sugar but I like to be proven otherwise. :) – dacwe Jan 26 '12 at 11:21
  • I'm not sure what you're looking for here. The libraries "that have been imported (using the import keyword)" can be different to those that are "available during the execution". For example, you can compile against a library (import its types), but not include it on the classpath at runtime. Or, you can add extra jars to the runtime classpath which you don't use. Also, unused imports do not appear in the bytecode. – Martin Ellis Jan 26 '12 at 11:29
  • @martiell Actually *no* imports appear in bytecode. – bezmax Jan 26 '12 at 11:30
  • http://stackoverflow.com/questions/5701305/how-to-get-all-imports-defined-in-a-class-using-java-reflection – Sergey Benner Jan 26 '12 at 11:35
  • @Max True in a sense. I mean that the names of classes that are imported and used will appear in the bytecode. That's not exactly the same (since you might use no imports and fully qualify all class references), but it might be a good enough approximation for whatever Alain is trying to do. – Martin Ellis Jan 26 '12 at 11:36
  • Sorry for not be enough clear. I want to get the lib "that have been imported (using the import keyword)" when I run the programm. I don't care of other default lib. But it seems not to be possible nowadays. I think the only ways is to parse the original source file. – alain.janinm Jan 26 '12 at 11:36
  • OK, I've suggested an answer that involves parsing source files. – Martin Ellis Jan 26 '12 at 11:59

2 Answers2

7

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
Martin Ellis
  • 9,603
  • 42
  • 53
  • Yeah!! Awesome! It works great. I've notices an issue by the way, if my class Main is within a package the `getImports()` return empty array. Will look for docs! Thanks for your help! – alain.janinm Jan 26 '12 at 14:36
2

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?

Natix
  • 14,017
  • 7
  • 54
  • 69