ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-09-11 03:01:57  阅读:217  来源: 互联网

标签:java reflection dynamic-loading


参见英文答案 > How do you dynamically compile and load external java classes?                                     2个
我将类名存储在属性文件中.我知道类存储将实现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

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

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,reflection,dynamic-loading
来源: https://codeday.me/bug/20190911/1803434.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有