Dynamic compilation and java Dynamic Compilation

Source: Internet
Author: User

Dynamic compilation and java Dynamic Compilation

Sometimes we need to compile the dynamically generated content.

For example, if you input a piece of code in the client browser to the server, the server obtains the code for compilation.

Two Methods of dynamic compilation:

(1) Call javac through Runtime and start a new process to operate on it.

Runtime runtime = Runtime. getRuntime ();

Process process = runtime.exe c ("javac-cp c:/myjava/HelloWorld. java ");

(2) dynamic compilation through JavaCompiler

JavaCompiler compiler = ToolProvider. getSystemJavaCompiler ();

Int result = compiler. run (null, sourceFile );

Parameter introduction:

First parameter: provide parameters for the Java compiler

Second parameter: Get the output information of the Java compiler.

Third parameter: receive error messages from the Java compiler

The fourth parameter is a variable array String []. One or more Java files can be imported.

Returned value: 0 indicates that the compilation is successful, and non-0 indicates that the compilation fails.

 

Below is a demo for your reference.

First, create a myjava folder under the root directory of drive D, and create a new java class named HelloWorld. java in the myjava folder.

1 package com. coscon. compiler; 2 3 import javax. tools. javaCompiler; 4 import javax. tools. toolProvider; 5 6 public class JavaCompilerTest {7 public static void main (String [] args) {8 JavaCompiler compiler = ToolProvider. getSystemJavaCompiler (); 9 int result = compiler. run (null, "d:/myjava/HelloWorld. java "); 10 System. out. println (result = 0? "Compiled successfully": "Compilation failed"); 11} 12}

The console prints the compilation successful, and the. class file appears in the myjava folder.

If we need to dynamically compile a String, for example, there is a String str = "public class Demo2 {public static void main (String [] args) {System in the Java class above. out. print (\ "compiled string \")}}";

We can store the above strings into a temporary file through the io stream. The suffix is. java, and dynamic compilation is performed by calling the JavaCompiler run method.

 

After dynamically compiled classes, sometimes we need to dynamically run these compiled classes.

There are also two methods:

(1) Call java through Runtime and start a new process to operate on it.

Runtime runtime = Runtime. getRuntime ();

Process process = runtime.exe c ("java-cp c:/myjava/HelloWorld ");

If you run the command directly, the console cannot obtain the printed information.

InputStream in = process. getInputStream ();

BufferedReader reader = new BufferedReader (new inputStreamReader (in ));

String info = "";

While (info = reader. readLine ())! = Null ){

System. out. println (info );

}

In this way, the interaction between two processes is realized.

(2) run the compiled class through reflection

URL [] urls = new URL [] {new URL ("file:/" + "d:/myjava /")};
URLClassLoader loader = new URLClassLoader (urls );
Class c = loader. loaderClass ("HelloWorld ");
// Call the mian method of the load class
Method m = c. getMethod ("main", String []. class );
M. invoke (null, (Object) new String [] {}); // call a static method. null can be passed. The second parameter must be converted to a strong one.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.