java - 如何以编程方式编译和实例化 Java 类?

标签 java reflection dynamic-loading

我将类名存储在属性文件中。我知道类存储将实现 IDynamicLoad。如何动态实例化类?

我现在有

     Properties foo = new Properties();
    foo.load(new FileInputStream(new File("ClassName.properties")));
    String class_name = foo.getProperty("class","DefaultClass");
    //IDynamicLoad newClass = Class.forName(class_name).newInstance();

newInstance 是否只加载已编译的 .class 文件?如何加载未编译的 Java 类?

最佳答案

How do I load a Java Class that is not compiled?

你需要先编译它。这可以通过 javax.tools API 以编程方式完成。 .这只需要 JDK安装在 JRE 之上的本地机器上。

这是一个基本的启动示例(抛开明显的异常处理):

// Prepare source somehow.
String source = "package test; public class Test { static { System.out.println(\"hello\"); } public Test() { System.out.println(\"world\"); } }";

// Save source in .java file.
File root = new File("/java"); // On Windows running on C:\, this is C:\java.
File sourceFile = new File(root, "test/Test.java");
sourceFile.getParentFile().mkdirs();
Files.write(sourceFile.toPath(), source.getBytes(StandardCharsets.UTF_8));

// Compile source file.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, sourceFile.getPath());

// Load and instantiate compiled class.
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
Class<?> cls = Class.forName("test.Test", true, classLoader); // Should print "hello".
Object instance = cls.newInstance(); // Should print "world".
System.out.println(instance); // Should print "test.Test@hashcode".

哪个产量像

hello
world
test.Test@ab853b

如果这些类实现某个接口(interface)已经在类路径中,那么进一步使用会更容易。

SomeInterface instance = (SomeInterface) cls.newInstance();

否则您需要参与Reflection API访问和调用(未知)方法/字段。


这与实际问题无关:

properties.load(new FileInputStream(new File("ClassName.properties")));

java.io.File 依赖于当前工作目录会导致可移植性问题。不要那样做。将该文件放在类路径中并使用 ClassLoader#getResourceAsStream()具有类路径相对路径。

properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("ClassName.properties"));

关于java - 如何以编程方式编译和实例化 Java 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2946338/

相关文章:

c++ - 如何找到库以使用 dlopen 动态加载它们

java - 无法在 Mac OS Yosemite 上安装 JDK 8u25

.net - 什么时候应该继承 System.Type

java - 构造一个在运行时确定类型的对象?

c++ - 动态加载和动态绑定(bind)的区别

c++ - 在 C++ 中禁用自动 DLL 加载

java - 如何获取当前正在运行的应用程序列表,如任务管理器,但使用 JAVA 在后台运行的应用程序除外?

java - Hibernate 卡在 tx.commit 上

java - robots.mousepress "Auto Clicker"如何让它停止?

java - 如何通过反射获取多重签名调用方方法?