Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 20th, 2012  |  syntax: None  |  size: 2.44 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. 参照网上其他童鞋的例子与介绍,修改了一下:
  2. import java.io.IOException;
  3. import java.lang.reflect.Method;
  4. import java.net.URI;
  5. import java.net.URISyntaxException;
  6. import java.util.Arrays;
  7.  
  8. import javax.tools.JavaCompiler;
  9. import javax.tools.JavaCompiler.CompilationTask;
  10. import javax.tools.JavaFileObject;
  11. import javax.tools.SimpleJavaFileObject;
  12. import javax.tools.StandardJavaFileManager;
  13. import javax.tools.ToolProvider;
  14.  
  15. /**
  16.  * 动态编译Java 源文件
  17.  *
  18.  * @author shenshouer
  19.  *
  20.  */
  21. public class CompilerTest {
  22.  
  23.         public static void main(String[] args) throws Exception {
  24.                 String source = "public class Main { " + "public static void main(String[]args) {"
  25.                                                 + "System.out.println(\"Hello World!\");" + "} " + "}";
  26.  
  27.                 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  28.                 StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
  29.                 StringSourceJavaObject sourceObject = new CompilerTest.StringSourceJavaObject("Main",
  30.                                                 source);
  31.                 Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(sourceObject);
  32.                
  33.                  // 获取编译类根路径,不然会报找不到类的错误
  34.                 String path = Class.class.getClass().getResource("/").getPath();
  35.                 Iterable< String> options = Arrays.asList("-d", path);
  36.                
  37.                 // 增加options参数
  38.                 // CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);
  39.                 CompilationTask task = compiler.getTask(null, fileManager, null, options, null, fileObjects);
  40.  
  41.                 boolean result = task.call();
  42.  
  43.                 if (result) {
  44.                         System.out.println("编译成功。");
  45.  
  46.                         ClassLoader loader = CompilerTest.class.getClassLoader();
  47.                         try {
  48.                                 Class<?> clazz = loader.loadClass("Main");
  49.                                 Method method = clazz.getMethod("main", String[].class);
  50.                                 // 修改调用参数,不然会报参数个数不对
  51.                                 // Object value = method.invoke(null, new Object[] {});
  52.                                 Object value = method.invoke(null, new Object[] {new String[]{}});
  53.                                 System.out.println(value);
  54.                         } catch (Exception e) {
  55.                                 e.printStackTrace();
  56.                         }
  57.                 }
  58.         }
  59.  
  60.         static class StringSourceJavaObject extends SimpleJavaFileObject {
  61.  
  62.                 private String content = null;
  63.  
  64.                 public StringSourceJavaObject(String name, String content) throws URISyntaxException {
  65.                         super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension),
  66.                                                         Kind.SOURCE);
  67.                         this.content = content;
  68.                 }
  69.  
  70.                 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
  71.                         return content;
  72.                 }
  73.         }
  74. }