Java Dynamic Compilation

Source: Internet
Author: User

First, the diagram:

Second, the Code demonstration

 PackageCom.hy;ImportJava.io.BufferedReader;ImportJava.io.FileReader;ImportJava.io.FileWriter;ImportJava.lang.reflect.Constructor;ImportJava.net.URL;ImportJava.net.URLClassLoader;ImportJavax.tools.JavaCompiler;ImportJavax.tools.JavaCompiler.CompilationTask;ImportJavax.tools.StandardJavaFileManager;ImportJavax.tools.ToolProvider;Importorg.junit.Test;/** *  * @authorAdministrator **/ Public classTest1 {@Test Public voidtest1 () {Try {            //to read a text into memoryFileReader fr=NewFileReader ("C:\\users\\administrator\\desktop\\news2.txt"); BufferedReader BR=NewBufferedReader (FR); String Line= ""; StringBuilder SB=NewStringBuilder ();  while(line = Br.readline ())! =NULL) {sb.append (line); Sb.append ("\ n");            } br.close ();                        Fr.close (); //generate a Java file under a pathString Path = System.getproperty ("User.dir") + "\\src\\com\\hy\\News2.java"; FileWriter FW=NewFileWriter (path);            Fw.write (Sb.tostring ());            Fw.flush ();                        Fw.close (); //compiling classes into class filesJavacompiler compiler = Toolprovider.getsystemjavacompiler ();//get the Java compilerStandardjavafilemanager FileManager = Compiler.getstandardfilemanager (NULL,NULL,NULL);//get a Java File ManagerIterable iterable = filemanager.getjavafileobjects (path);//to generate a specific Java file objectCompilationtask Compilationtask = Compiler.gettask (NULL, FileManager,NULL,NULL,NULL, iterable);//get a compilation taskCompilationtask.call ();//compilingFilemanager.close (); //load class into memoryurl[] URLs =Newurl[]{NewURL ("file:/" +system.getproperty ("User.dir") + "/src")};//load all Class (network path) below this pathURLClassLoader URLClassLoader =NewURLClassLoader (URLs); Class C= Urlclassloader.loadclass ("Com.hy.News2");                                    System.out.println (c); Constructor Constructor= C.getconstructor ();//Get Construction MethodNews2 news2 = (News2) constructor.newinstance ();//using a construction method to get an instanceNews2.sayhello ();//Calling Methods}Catch(Exception e) {e.printstacktrace (); }    }    }

Note: Here's a demonstration of dynamically generating a Java object from code text, using dynamic compilation!

Java Dynamic Compilation

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.