182手机网 趣闻 动态编译实例代码

动态编译实例代码

,动态编译实例代码

动态编译实例代码。非常简单。我看也不用注解了吧。
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import java.io.File; static com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
public static void main(String[] args){
try{
File file = File.createTempFile(“RunTime”,”.java”,new File(System.getProperty(“user.dir”)));
String filename = file.getName();
String classname = filename.substring(0,filename.indexOf(‘.’));
PrintWriter out = new PrintWriter(new FileOutputStream(file));
out.println(“public class “+classname+”{“);
out.println(“public static void main(String[] args){“);
out.println(“System.out.println(\”Yes!\”);”);
out.println(“}}”);
out.flush();
out.close();
String[] arg = new String[]{“-d”,System.getProperty(“user.dir”),filename};
int status = javac.compile(arg);
Class cls = Class.forName(classname);
Method main = cls.getMethod(“main”, new Class[]{String[].class} );
main.invoke(null, new Object[] { new String[0] });
file.delete();
file = new File(classname+”.class”);
file.delete();
}catch(Exception e){e.printStackTrace();} }

本文来自网络,不代表182手机网立场,转载请注明出处:https://www.182shouji.com/html/happy/2020/0319/135532.html

作者: dawei

【声明】:182手机网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。
联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部