java动态编译

 

 

 

 

package src;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

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

public class Demo01 {

	public static void main(String[] args) throws IOException {
		// 动态编译 
		JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
		int result=comp.run(null, null, null, "D:\\codes\\javaProjects\\test\\Test.java");
		System.out.println(result==0?"运行成功":"运行失败");
		
		Runtime run = Runtime.getRuntime();
		Process process= run.exec("java -cp D:/codes/javaProjects/test Test");
		InputStream in= process.getInputStream();
		
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String info = "";
		while((info=reader.readLine())!=null) {
			System.out.println(info);
		}
		
		
	}

}

  

posted @ 2023-07-04 13:40  巨兽~墨菲特  阅读(24)  评论(0编辑  收藏  举报