Revision: 20926
                            
                                                            
                                    
                                        
Initial Code
                                    
                                    
                                                            
                                    
                                        
Initial URL
                                    
                                    
                                                            
                                    
                                        
Initial Description
                                    
                                    
                                                            
                                    
                                        
Initial Title
                                    
                                    
                                                            
                                    
                                        
Initial Tags
                                    
                                    
                                                            
                                    
                                        
Initial Language
                                    
                                    
                                                    
                        at December 1, 2009 09:09 by akostajti
                            
                            Initial Code
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();
    }
}
                                Initial URL
http://cesjava.freeblog.hu
Initial Description
this simple snippet compiles a Java source file.
Initial Title
programmatically compiling a Java source file
Initial Tags
java
Initial Language
Java