动态编译java程序实例
类别: JAVA教程
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);
}
}
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学习笔记(一)--熟悉开发环境
- 下一篇: Java开源项目Hibernate快速入门
-= 资 源 教 程 =-
文 章 搜 索