JAVA动态编译

思路:

1、创建java代码的字符串

2、将代码字符串通过IO流以文件形式存放到硬盘

3、将生产的文件进行编译成class

4、通过反射调用该class

注意:

生成的class文件要在上述代码能够调用到的位置

源码:

 1 import java.io.File;
 2 import java.io.FileWriter;
 3 import java.io.IOException;
 4 import java.net.URL;
 5 import java.net.URLClassLoader;
 6 import java.util.Arrays;
 7 
 8 import javax.tools.JavaCompiler;
 9 import javax.tools.JavaFileObject;
10 import javax.tools.StandardJavaFileManager;
11 import javax.tools.ToolProvider;
12 
13 public class TestCompiler {
14 
15     private final static String className = "Compiler";
16     private final static String classLocation = System.getProperty("user.dir") + "\\bin";
17     private final static String fileName = classLocation + "\\" + className + ".java";
18     private static StringBuffer javaCode = null;
19     
20     static {
21         
22         String br = "\r\n";
23         javaCode = new StringBuffer();
24         javaCode.append("public class " + className + "{" + br
25                 + "\tpublic static void main(String[] args) {" + br
26                 + "\t\tSystem.out.println(\"Hello World!\");" + br + "\t}"
27                 + br + "}");
28     }
29 
30     private static void getJavaCode(StringBuffer buffer) {
31 
32         if (buffer != null && !buffer.toString().equals("")) {
33             javaCode = buffer;
34         } 
35     }
36 
37     private static void createFileCode() {
38 
39         File file = new File(classLocation);
40 
41         if (!file.exists()) {
42             file.mkdir();
43         }
44 
45         try {
46             FileWriter writer = new FileWriter(new File(fileName));
47             writer.write(javaCode.toString());
48             writer.flush();
49             writer.close();
50         } catch (IOException e) {
51             e.printStackTrace();
52         }
53     }
54 
55     public static void compilerJavaCode() {
56         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
57         StandardJavaFileManager fileManager = compiler.getStandardFileManager(
58                 null, null, null);
59         Iterable<? extends JavaFileObject> sourcefiles = fileManager.getJavaFileObjects(fileName);
60         Iterable<String> options = Arrays.asList("-d", classLocation);
61         compiler.getTask(null, fileManager, null, options, null, sourcefiles)
62                 .call();
63         try {
64             fileManager.close();
65         } catch (IOException e) {
66             e.printStackTrace();
67         }
68     }
69 
70     public static void execute() {
71         URL[] urls;
72         try {
73             urls = new URL[] { new URL("file:" + classLocation) };
74             URLClassLoader loader = new URLClassLoader(urls);
75             Class<?> c = loader.loadClass(className);
76             c.getMethod("main", String[].class).invoke(null,
77                     (Object) new String[] { "" });
78         } catch (Exception e) {
79             e.printStackTrace();
80         }
81     }
82 
83     public static void main(String[] args) {
84         // 创建java代码
85         getJavaCode(null); 
86         // 输出到文件
87         createFileCode();
88         // 编译
89         compilerJavaCode();
90         // 执行
91         execute();
92     }
93 }
View Code
posted @ 2016-06-23 10:13  〃微笑的恶魔  阅读(212)  评论(0编辑  收藏  举报