Wednesday, October 18, 2006

Java:Programmatically compiling a java file

The class com.sun.tools.javac.Main.compile provides the programming interface for javac.
sample code is :

int errorCode = com.sun.tools.javac.Main.compile(new String[] {
"-classpath", "bin",
"-d", "/temp/dynacode_classes",
"dynacode/sample/Foo.java" });

errorCode return 0 for Sucess and some non-zero number for any compilation failure.

Also to print the compilation error messages to an o/p file,
it has another overloaded method.

// Defined in com.sun.tools.javac.Main
public static int compile(String[] args);
public static int compile(String[] args, PrintWriter out);

No comments: