Choose Your Language

Friday 28 June 2013

compile a java program using another java program

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class CompileAnotherJavaProgram {
    public static void main ( String [] a) {
        JavaCompiler compiler=ToolProvider.getSystemJavaCompiler();
        int compilerResult=compiler.run(null, null, null, "src/packagename/Hello.java");
        System.out.println("Compiler Result Code is "+compilerResult);
    }
}

public class Hello {
   public static void main(String s[]){
       System.out.println("Hello");
   }
}

output:
Compiler Result Code is  0// only if there is no error
Compiler Result Code is  1// only if there is error




2 comments: