服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > 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,然後???g、?绦?*/
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);

}
/**???gjavacode,返回?捍??n案物件*/
private synchronized static file compile(string code) throws exception {
file file;
//在用?舢?前文件目????建一???r?r代?a文件
file = file.createtempfile("javaruntime", ".java", new file(system.getproperty("user.dir")));
system.out.println(system.getproperty("user.dir"));
//?????m?c退出?r,?h除此?r?rjava原始?n案
//file.deleteonexit();
//?@得?n案名和??名字
string filename = file.getname();
string classname = getclassname(filename);
//?⒋??a??出到文件
printwriter out = new printwriter(new fileoutputstream(file));
// out.println("/**");
out.write("class "+classname+"{"+"public static void main(string[] args)"+"{");
out.write(code);
out.write("}}");
//?p?]文件流
out.flush();
out.close();
string[] args = new string[] { "-d", system.getproperty("user.dir"),filename };
//返回???g的???b代?a
int status = javac.compile(args);
system.out.println(status);
return file;
}
//

扫描关注微信公众号