代码非常简单,,要引入tool.jar这个包
com.sun.tools.javac.Main javac = new Main();
File file =new File("E:/work/JavaGGDS/bin/Test.java");// new File("Test",".java", );
if(file.exists())file.delete();
FileWriter fw=new FileWriter(file);
fw.write("public class "+file.getName().substring(0,file.getName().lastIndexOf("."))+" { public static void a(){System.out.println(\"dddddddd\");}}");
fw.flush();
fw.close();
// String[] arg0=new String[]{"-d",System.getProperty("user.dir"),file.getName()};
String[] arg0=new String[]{"-d","E:/work/JavaGGDS/bin/","E:/work/JavaGGDS/bin/"+"Test.java"};
int status=javac.compile(arg0);
System.out.println(status);
Class cls = Class.forName(file.getName().substring(0,file.getName().lastIndexOf(".")));
cls.getMethod("a").invoke(null);