Java动态编译和动态执行

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;
  7. import com.sun.tools.javac.*;
  8. public class MainComplier {
  9.     private static Main javac =  new com.sun.tools.javac.Main();
  10.     public static void main(String[] args) throws IOException, ClassNotFoundException, 
  11.     SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, 
  12.     InvocationTargetException {
  13.         String classString = "public static void main(String[] args){/n" +
  14.                              " System.out.println(/"Hi gal , I love u !!! /");/n" + 
  15.                              " }/n" +
  16.                              "}";
  17.         String path=System.getProperty("user_dir");//获取当前项目路径
  18.         File file=new File(path,"Test.java");      //在当前路径创建空文件
  19.         String filename = file.getName();  
  20.         System.out.println("FileName:" + filename);
  21.         int index = filename.indexOf(".");
  22.         String className = filename.substring(0,index); //获取类名
  23.         System.out.println("ClassName:" + className);
  24.         PrintWriter out = new PrintWriter(new FileOutputStream(file)); //在文件里写入代码
  25.         out.println("public class " + className + "{");
  26.         out.println(classString);
  27.         out.flush();
  28.         out.close();
  29.         String[] test = new String[] {"-d", System.getProperty("user.dir"),filename }; //编译命令
  30.         int status = javac.compile(test); //编译代码
  31.         System.out.println("Status:" + status);
  32.         Class myClass = Class.forName("MyFile");//获取.class的实例
  33.         Class[] params=new Class[2];   //将参数的类型描述好
  34.         Integer a=new Integer(10);
  35.         Integer b=new Integer(20);
  36.         params[0]=a.getClass();
  37.         params[1]=a.getClass();
  38.         Method myMethod =myClass.getMethod("add"params);//调用实例中哪个方法
  39.         Object[] myArgs=new Object[2];//将参数的数值设置好
  40.         myArgs[0]=a;
  41.         myArgs[1]=b;
  42.         myMethod.invoke(myClass,myArgs);//调用.class的实例的方法
  43.         
  44.     }
  45. }
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. public class MyFile {
  6.     public static void main(String[] args) throws IOException {
  7.         String path=System.getProperty("user_dir");
  8.         File file=new File(path,"FileD.txt");
  9.         file.createNewFile();
  10.         PrintWriter out=new PrintWriter(new FileOutputStream(file));
  11.         out.println("hello");
  12.         out.flush();
  13.         out.close();
  14.         System.out.println("sunsnow");
  15.         MyFile test=new MyFile();
  16.         test.add(10, 13);
  17.     }
  18.     public static Integer add(Integer a,Integer b)
  19.     {   
  20.         System.out.println(new Integer(a+b));
  21.         return new Integer(a+b);
  22.     }
  23.     
  24. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值