简体   繁体   English

如何打印导入的java库?

[英]How to print imported java libraries?

Is there a way to print within a Java Code the libraries that has been imported, and available during the execution? 有没有办法在Java代码中打印已导入并在执行期间可用的库?

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 . 我需要打印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: 如果您需要源代码中使用的实际导入(而不是使用字节码中的信息),您可以使用名为QDox的库来解析源代码并获取您使用的导入列表:

Main.java 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? 无论如何,你需要什么这样的功能?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM