通过JavaCompiler动态编译和运行

package com.buaa.reflectTest.dynamic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class Demo01 {
	public static void main(String[] args) throws Exception{
		
		String code = "public class Hello{" +
							"public static void main(String[] args){" +
								"System.out.println(\"HelloWorld\");" +
							"}" +
						"}";
		File file = new File("e:/xxxjava/Hello.java");
		if(!file.exists()){
			file.getParentFile().mkdirs();
			file.createNewFile();
		}
		OutputStream os = new FileOutputStream(file);
		os.write(code.getBytes(), 0, code.length());
		os.flush();
		os.close();
		
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
		//直接从文件中获取
		/**compiler.run(1,2,3,4)
		 * @parameters:
			1.in → "standard" input; use System.in(键盘输入) if null
			2.out → "standard" output; use System.out(输出到控制台) if null
			3.err → "standard" error; use System.err if null
			4.arguments → arguments to pass to the tool
		 */
		int result = compiler.run(null, null, null, "e:/HelloWorld.java");
		//将Java代码以字符串的形式,先写入文件,然后再通过编译器读取出来,达到动态的效果。
		int result2 = compiler.run(null, null, null, "e:/xxxjava/Hello.java");
		System.out.println(result==0?"编译成功":"编译失败");
		System.out.println(result2==0?"编译成功":"编译失败");
		
//通过Runtime类动态运行编译好的类
		Runtime rt = Runtime.getRuntime();
		Process pro = rt.exec("java -cp e:/xxxjava Hello");//实际上已经执行了
		
		//让结果输出到控制台
		InputStream in = pro.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String temp = "";
		while((temp=reader.readLine())!=null){
			System.out.println(temp);
		}
		
		
//通过类加载器来动态运行编译好的类
		URL[] urls = new URL[]{new URL("file:/" + "e:xxxjava/")};
		URLClassLoader loader = new URLClassLoader(urls);
		Class c = loader.loadClass("Hello");
		//调用加载类的main方法
		Method m = c.getMethod("main", String[].class);
		m.invoke(null, (Object)new String[]{"aa","bb"});
		//注意上面的代码,如果不加(Object)转型的话,
		//则会编译成:m.invoke(null,"aa","bb"),就发生了参数个数不匹配的问题。
		//因此,必须要加上(Object)转型,避免这个问题。
		
	}
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值