|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
import java.io.*; import java.lang.reflect.Method; import com.sun.tools.javac.*; public class RuntimeCode { private static Main javac = new Main(); /**等待用?糨?入JavaCode,然後??诅、?绦歇/ public static void main(String[] args) throws Exception { String code = ""; DataInputStream bd = new DataInputStream(System.in); byte[] brray= new byte[200]; int i = bd.read(brray); code = new String(brray,0,i); run(compile(code)); System.out.print(code); } /**??诅JavaCode,返回?捍??n案物件*/ private synchronized static File compile(String code) throws Exception { File file; //在用?舢?前文件目????建一??乓?r代瘁文件 file = File.createTempFile("JavaRuntime", ".java", new File(System.getProperty("user.dir"))); System.out.println(System.getProperty("user.dir")); //?????M?C退出?r,?h除此乓?rjava原始?n案 //file.deleteOnExit(); //?@得?n案名和??名字 String filename = file.getName(); String classname = getClassName(filename); //?⒋?瘁??出到文件 PrintWriter out = new PrintWriter(new FileOutputStream(file)); // out.println("/**"); out.write("class "+classname+"{"+"public static void main(String[] args)"+"{"); out.write(code); out.write("}}"); //晷檩文件流 out.flush(); out.close(); String[] args = new String[] { "-d", System.getProperty("user.dir"),filename }; //返回??诅的???B代瘁 int status = javac.compile(args); System.out.println(status); return file; } //忒行程序 private static synchronized void run(File file) throws Exception { String filename = file.getName(); String classname = getClassName(filename); //?????M?C退出?r,?h除此乓?r??诅的??文件 new File(file.getParent(),classname + ".class").deleteOnExit(); try { Class cls = Class.forName(classname); //映射main方式 Method main = cls.getMethod("main", new Class[] { String[].class }); //?绦许ain方式 main.invoke(null, new Object[] { new String[0] }); } catch (SecurityException se) { } } private static void debug(String msg) { System.err.println(msg); } private static String getClassName(String filename) { return filename.substring(0, filename.length() - 5); } } 返回类别: 教程 上一教程: java虚拟机学习笔记2 下一教程: Java 性能优化技巧集锦 (3) 您可以阅读与"动态编译java程序实例"相关的教程: · 动态编译JAVA程序 · 动态编译实例代码 · JAVA的动态编译和静态编译 · 在java中利用动态编译实现eval · 一个JAVA的初学者如何准确编译HELLOWORLD程序 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |