/ Published in: Java
this simple snippet compiles a Java source file.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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 */ /** * compiles a java source file with the given <code>fileName</code> * * @param fileName */ /* * the compiler will send its messages to this listener */ DiagnosticListener listener = new DiagnosticListener() { public void report(Diagnostic diagnostic) { } }; //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(); } }
URL: http://cesjava.freeblog.hu