在程序中编译 Java 源码,程序编译java源码,package ex.t
分享于 点击 49955 次 点评:115
在程序中编译 Java 源码,程序编译java源码,package ex.t
package ex.tajti.tools;import javax.tools.Diagnostic;import javax.tools.DiagnosticListener;import javax.tools.JavaCompiler;import javax.tools.JavaFileObject;import javax.tools.StandardJavaFileManager;import javax.tools.ToolProvider;/** * * @author ákos tajti */public class Compiler { /** * compiles a java source file with the given <code>fileName</code> * * @param fileName */ public void compile(String fileName) { /* * the compiler will send its messages to this listener */ DiagnosticListener listener = new DiagnosticListener() { public void report(Diagnostic diagnostic) { System.err.println("gond: " + diagnostic.getMessage(null)); System.err.println("sor: " + diagnostic.getLineNumber()); System.err.println(diagnostic.getSource()); } }; //getting the compiler object JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager manager = compiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> files = manager.getJavaFileObjects(fileName); JavaCompiler.CompilationTask task = compiler.getTask(null, manager, listener, null, null, files); // the compilation occures here task.call(); }}//该片段来自于http://byrx.net
用户点评