Home Java Java-exception Example to compile a java file with the help of the program
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

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

Advertisement
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

     

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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 22, 2008

Related Tutorials

Ask Questions?    Discuss: Example to compile a java file with the help of the program  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com