Java Note 11: dynamic compilation

Source: Internet
Author: User

Jdk6.0 introduces a new function of dynamic compilation, which allows you to dynamically write a class in the program and then compile it. After being compiled into a class file, you can load the dynamically compiled class into the memory by loading the class. Of course, it can also use the runtime class to call the javac command for dynamic compilation.

The main steps of the dynamic compilation class are as follows:

① Write a string, which is the full content of the class to be compiled.

② Write the content of the string to the specified package under the project through the output stream.

③ Compile the created Java file.

The classes involved in dynamic compilation include javacompiler and standardjavafilemanager. For more information about how to compile these classes, see the following example.

4. Load the compiled bytecode into the memory and perform operations on it.

1 package COM. compiler; 2 Import Java. io. *; 3 Import Java. lang. reflect. invocationtargetexception; 4 Import java.net. uri; 5 import java.net. URL; 6 Import java.net. urlclassloader; 7 Import Java. util. arrays; 8 9 Import javax. tools. javacompiler; 10 Import javax. tools. simplejavafileobject; 11 import javax. tools. standardjavafilemanager; 12 Import javax. tools. javafileobject; 13 Import javax. tools. toolprovide R; 14 15 public class compilerapitester {16 Private Static string java_source_file = "dynamicobject. java "; 17 Private Static string java_class_file =" dynamicobject. class "; 18 Private Static string java_class_name =" dynamicobject "; 19 20 public static void main (string [] ARGs) throws ioexception, interruptedexception {21 // create the Java file 22 string TR = "\ r \ n"; 23 string source = "package COM. compiler; "+ Tr + 24 "public class" + java_class_name + "{" + Tr + 25 "public static void main (string [] ARGs) {" + Tr + 26 "system. out. println (\ "Hello world! \ ");" + Tr + 27 "}" + Tr + 28 "}"; 29 string filename = system. getproperty ("user. dir ") +" \ SRC \ com \ compiler \ "+ java_source_file; 30 filewriter fw = new filewriter (filename ); // character output stream 31 printwriter PW = new printwriter (FW); // transfers the byte output to printwriter32 PW. write (source); 33 PW. close (); 34 // compile Java file 35 javacompiler compiler = toolprovider. getsystemjavacompiler (); 36 standardjavafilemanager filemanager = compiler. getstandardfilemanager (null, null, null); 37 iterable sourcefiles = filemanager. getjavafileobjects (filename); 38 // specifies the location where the compiled file is stored. If this parameter is not specified, the compiled file will be 39 in the same folder as the Java source file. // in this case, when the class is loaded, Java will be reported. lang. classnotfoundexception40 iterable <string> Options = arrays. aslist ("-d", system. getproperty ("user. dir ") +" \ webroot \ WEB-INF \ Classes "); 41 compiler. gettask (null, filemanager, null, options, null, sourcefiles ). call (); 42 filemanager. close (); 43 // Method 2: 44/* runtime = runtime. getruntime (); 45 runtime.exe C ("javac-d" + system. getproperty ("user. dir ") +" \ webroot \ WEB-INF \ Classes "+ filename); 46 thread. sleep (1000); * // because this method calls a thread to get the compilation, you need to let the main thread sleep for a while. Otherwise, the class will be loaded before the main thread is compiled, exception that cannot be found in the reported class 47 try {48 // load the class to memory 49 // Method 1: 50 // Class C = Class. forname ("com. compiler. "+ java_class_name); 51 // Method 2: 52 // Class C = classloader. getsystemclassloader (). loadclass ("com. compiler. "+ java_class_name); 53 // method 3: 54 URL [] URLs = new URL [] {new URL (" file:/"+ system. getproperty ("user. dir ") +"/src ")}; 55 urlclassloader loader = new urlclassloader (URLs); 56 Class C = loader. loadclass ("com. compiler. "+ java_class_name); 57 // call the main method of the loading class 58 C. getmethod ("Main", string []. class ). invoke (null, (object) New String [] {"A"}); 59} catch (exception e) {60 // todo auto-generated catch block61 E. printstacktrace (); 62} 63} 64 65}

 

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.