79 dynamic compilation and dynamic operation

Dynamic compilation and dynamic operation

In such a scenario: We designed a web page that allows the user to enter java code on the page, the results obtained after submitting java code. This feature, users do not need to install jre or jdk, you can get the results to compile java run. This function, it must rely on dynamic compilation and dynamic operation.

This design: the user enters the code, after submitting, through the network spread into our servers, we will accept the stream, into the corresponding java file, then call the compiler to compile it, and then call the Runtime class loader or its corresponding execution class file, and finally we will post the results, the flow returns to the user through the complete function.

So this process: the process of invoking the compiler and calling class loader is the process of dynamic compilation and dynamic operation.

How dynamic compilation in the program?

We get JavaCompiler compiler tools ToolProvider class, you can compile the file specified.

Compiler = ToolProvider.getSystemJavaCompiler JavaCompiler (); 
			int the Result = compiler.run (null, null, null, "c: /myjava/HelloWorld.java"); 
			System.out.println (the Result == 0 "compiled successfully"?: "compilation failed");

  

Caveats: pre run () method api three parameters refer to the method. The fourth parameter passed in the full path of the file when java. The results returned by the method is 0 to compile success, otherwise fail to compile.

How to run a dynamic class file in the program?

There are two ways to dynamically loaded class files, which are the main method of reflection type Runtime class file corresponding to operation.

Runtime Class

Each program has a corresponding run of Runtime objects, we can get it through Runtime.getRuntime (). With this object, we can call the class file to obtain a process (Process Object), the process execution does not appear in the current console (if the loaded class has a statement in the console output words). But we can get this process standard output stream (general value System.out, otherwise I do not know), can be exported to a file or stream network.

//动态运行
			Runtime runtime = Runtime.getRuntime();
			Process process = runtime.exec("java -cp c:/myjava HelloWorld");
			InputStream is = process.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String line = "";
			while(null!=(line = br.readLine())) {
				System.out.println(line);
			}

  

Caveats: exec () method is passed a command system, when here we pass the "java -cp c: / myjava HelloWorld", note that only space there is no "/" symbol between myjava and HelloWorld, and class .class file without suffix.

Standard input and standard output stream

I do not know whether there are other standard input and output streams.

public static final InputStream in "standard" input stream.
public static final PrintStream out "standard" output stream.
IS = the System.in the InputStream;
PrintStream PS = the System.out;

Reflection mechanism to load class file

The use of reflection we can be more flexible loading class files through a URL array we can specify a folder, and then use the URLClassLoader class to obtain a class loader, the loader can load any class specified folder. This result was loaded Class object (reflector) class file. Then we get the main method of Class objects execute the main method, skill load the class files. But after loading it does not seem to correspond to the output stream ... emmm

URL[] urls = new URL[] {new URL("File:/c:/myjava/")};
			URLClassLoader loader = new URLClassLoader(urls);
			Class c = loader.loadClass("HelloWorld");
			Method m = c.getMethod("main", String[].class);
			m.invoke(null, (Object)new String[] {});//这里要注意!

  

Note: When calling invoke () method, the first argument is null, I do not know why. The argument list for the second parameter of the method, but this method parameter is an array, then when an incoming argument, be sure to turn stronger Object type, or elements of the array will be split as the actual method of parameters.

Guess you like

Origin www.cnblogs.com/Scorpicat/p/12150035.html