Simple to understand Java dynamic compiler

Java 跟Javax

  1. Through the above explanation can clearly know javax also a part of the standard API, not all represent the extension of the meaning of, and for the Internet, said javax j2ee represents this part, in fact, this statement is the title, it should be said that problems left over by history is more appropriate now. java is a java j2sdk in the library, which is the Java Development kit. It also provides some basic things, such as libraries io libraries, desktop programs, such as awt. Collections library (such as Collection, List, Map), etc. The most basic of these libraries.

  2. java package is extended javax as j2ee class libraries, including servlet, jsp, ejb, some database related things, XML and the like.

  3. You can refer to the links below, is a j2sdk API , is a J2EE API look, you will be able to better understand.

  4. java and javax are Java API packages, the package is the core java, javax x is the extension of the meaning, that is, expansion packs, each of its usefulness and have java

Java dynamic compiler

  1. After JDK6.0 introduces dynamic compilation mechanism.
  2. Dynamic compilation scene can be implemented on the browser written in Java code, and then uploaded to the server running the compiler with such often see online version of Java web development environment.

Java dynamic compilation method.

1. Runtime call javac start a new process to perform the operation.

Runtime run = Runtime.getRuntime();
Process process = run.exec("javac -cp d:/myjava/ HelloWorld.java")

2. JavaCompiler dynamic compilation

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null, "c:/HelloWorld.java"); // 执行 指定脚本下Java 代码

Overall demo

package mytest;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * 测试java的动态编译
 * @author ljj
 *
 */
public class Client {
	public static void main(String[] args) throws Exception {

		// 通过IO流操作,将字符串存储成一个临时文件(Hi.java),然后调用动态编译方法!简单了解下
		String str = "public class Hi {public static void main(String[] args){System.out.println(\"HaHa,sowhat!\");}}";

		// 动态编译
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
		int result = compiler.run(null, null, null, "c:/HelloWorld.java"); // 执行 指定脚本下Java 代码
		System.out.println(result==0 ? "编译成功":"编译失败");


		// 通过Runtime.getRuntime() 运行启动新进程执行 编译好的类
		Runtime run = Runtime.getRuntime();
        Process process = run.exec("java -cp  c:/    HelloWorld");
        InputStream in = process.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String info = "";
		while((info=reader.readLine())!=null){
			System.out.println(info);
		}

		// 通过反射运行编译好的类
		try {
			URL[] urls = new URL[] {new URL("file:/" + "C:/")};
			URLClassLoader loader = new URLClassLoader(urls);
			Class c = loader.loadClass("HelloWorld");
			//调用加载类的main方法
			Method m = c.getMethod("main",String[].class);
			m.invoke(null, (Object)new String[]{});  //  因为是静态方法 所以不需要对象,传参的时候是数组 但是需要转化为 Object
			//由于可变参数是JDK5.0之后才有。
			//m.invoke(null, (Object)new String[]{});会编译成:m.invoke(null,"aa","bb"),就发生了参数个数不匹配的问题。
			//因此,必须要加上(Object)转型,避免这个问题。
			//public static void main(String[] args)

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

reference

demo
dynamic compilation

Published 443 original articles · won praise 922 · Views 1.24 million +

Guess you like

Origin blog.csdn.net/qq_31821675/article/details/103313962