JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
int run(InputStream in, OutputStream out, OutputStream err, String... arguments) |
int results = tool.run(null, null, null, "test.java"); |
import java.io.*; import javax.tools.*; public class test_compilerapi { public static void main(String args[]) throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); int results = compiler.run(null, null, null, "test.java"); System.out.println((results == 0)?"编译成功":"编译失败"); // 在程序中运行test Runtime run = Runtime.getRuntime(); Process p = run.exec("java test"); BufferedInputStream in = new BufferedInputStream(p.getInputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String s; while ((s = br.readLine()) != null) System.out.println(s); } } public class test { public static void main(String[] args) throws Exception { System.out.println("JavaCompiler测试成功!"); } } |
test.java:9: 找不到符号 符号: 方法 printlnln(java.lang.String) 位置: 类 java.io.PrintStream System.out.printlnln("JavaCompiler测试成功!"); ^ 1 错误 编译失败 |