问题描述
我想制作一个命令行实用程序,该实用程序根据外部类文件的反映进行一些操作。我会传入.class文件或源文件(可能是通配符)的路径。在执行过程中的某个时刻,我需要为每个类获取Class对象,而不事先知道它们的包名称。要做什么呢?我应该如何看待入门?我也可以访问类的源文件。
I want to make a command-line utility that does some operations based on reflection of external class files. I would pass in a path to .class files or source files(possibly wildcards). At some point during the execution, I need to get Class objects for each class, not knowing their package names beforehand. What would it take to do this? What can I look at to get started? I also have access to the classes' source files.
这是在Java 1.6中。
this is in java 1.6.
推荐答案
您将需要创建一个并使用创建一个您已阅读的文件。 是有趣的是,这类文章甚至描述了源代码的编译。
You will need to create a ClassLoader
and use defineClass
to create a Class
from the file you've read. Add dynamic Java code to your application is an interesting to read about such things, it even describes the compilation of the sources.
这篇关于如何从文件系统加载任意java .class文件并进行反思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!