Example to compile a java file with the help of the program

Here we are describing the way to compile the java file with the help of the java program.

Example to compile a java file with the help of the program

Here we are describing the way to compile the java file with the help of the java program.

Example to compile a java file with the help of the program

Example to compile a java file with the help of the program

     

In this Tutorial  we are describing you the way to compile the java file with the help of the java program. The steps involved for compiling the java file from the program are described below: For this we are taking a class Compile Hello, Inside the main method we include the following -

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler():-Thi Creating an interface named compiler.JavaCompiler is an  Interface to invoke Java programming language compilers for programs.

int result = compiler.run(null, null, null,"src/Hello.java"):-This is the method of the interface JavaCompiler.This method generally invokes  the tool to run with the given I/O channels and arguments.

CompileHello.java 

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class CompileHello {
  public static void main(String[] args) {
  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  int result = compiler.run(null, null, null,"src/Hello.java");
  System.out.println("Compile result code = " + result);
  }
}

Output of the program

Compile result code = 0

Note:-The output shown above is not sufficient .you can check that the class will be generated to the particular directory which you have given above.

Download Sourcecode