Thursday, December 13, 2007

Programmatically compiling the Java program using Compiler API

Sample code to programmatically compile a Java program.
public class CompilerExample {
    public static void main(String[] args) {
        String fileToCompile = "c:" + java.io.File.separator
                + "HelloWorld.java";
        JavaCompiler compiler =      ToolProvider.getSystemJavaCompiler();
        int compilationResult = compiler.run(null, null, null, fileToCompile);
        if (compilationResult == 0) {
            System.out.println("Compilation is successful");
        } else {
            System.out.println("Compilation Failed");
        }
    }
}

1 comment:

  1. thank you very much for this piece of code........can you please givesippet for changing background color in that jpg
    adavane thanks

    ReplyDelete