Java Compiler tools: how you can compile a Java source from inside a Java program : Java Compiler tools « JDK 6 « Java





Java Compiler tools: how you can compile a Java source from inside a Java program

 


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.tools.JavaCompilerTool;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.JavaCompilerTool.CompilationTask;

public class JDK6JavaCompilerDemo {

    public static void main (String[] args) {
        String sourceFile = "c:/HelloWorld.Java";
        JavaCompilerTool compiler = ToolProvider.getSystemJavaCompilerTool ();
        StandardJavaFileManager fileManager =
                compiler.getStandardFileManager (null);

        // prepare the source file(s) to compile
        List<File> sourceFileList = new ArrayList <File> ();
        sourceFileList.add (new File (sourceFile));
        Iterable<? extends JavaFileObject> compilationUnits =fileManager.getJavaFileObjectsFromFiles (sourceFileList);
        CompilationTask task = compiler.getTask (null,fileManager, null, null, null, compilationUnits);
        task.run ();
        boolean result = task.getResult ();
        if (result) {
            System.out.println ("Compilation was successful");
        } else {
            System.out.println ("Compilation failed");
        }
        try {
            fileManager.close ();
        } catch (IOException e) {
        }
    }
}

/*
 * import Java.util.Date;
public class HelloWorld {
    public static void main (String[] args) {
        Date date = new Date (1234, 11, 12);
        System.out.println ("[" + date + "] Hello World !");
    }
}
 */

        










Related examples in the same category

1.Compile a Java code
2.Compile a Java file with JavaCompiler
3.Compiling from Memory