View RSS Feed

My Java Tips

Compiling a Java class using JavaCompiler

Rate this Entry
by , 11-28-2011 at 07:47 PM (2869 Views)
Java 6 introduces a way to compile Java classes from a Java class. In this post, I will present an example to show how this is done.


I have a Test.java file:

Java Code:
public class Test{

public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Hello World");
callMe();
}
    public static void callMe(){
     System.out.println("Hello Babar dost");
    }
}
I want to compile it from my Java class. This is possible in Java 6 using javax.tools package. Lets see how this is done:

Java Code:
import javax.tools.*;
import java.io.*;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        File file = new File("C:\Test.java");

        File files1 = file ; // input for first compilation task
       JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

       Iterable compilationUnits1 =
           fileManager.getJavaFileObjectsFromFiles(Arrays.asList(files1));
       compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();
    }
}
I compiled Main.Java and executed it. After execution, I could see Test.class in the c drive which means that Test.java was compiled successfully.


You may want to compile more than one classes from a Java class using javax.tools package.Only difference is that I will use a File array and will put the file objects referring to Java classes.

Java Code:
import javax.tools.*;
import java.io.*;
import java.util.Arrays;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Hello World");

        File file1 = new File("C:\ClassA.java");
        File file2= new File("C:\ClassB.java");
        File []fileArray;
        fileArray = new File[2];

        fileArray[0]= file1;
        fileArray[1]= file2;  

       // File files1 = file ; // input for first compilation task
       JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

       Iterable compilationUnits1 =
           fileManager.getJavaFileObjectsFromFiles(Arrays.asList(fileArray));
       compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();
    }

}
I compiled this class and on execution, got the desired results. Try at your end and see the power of Java 6.0 (MUSTANG).

Submit "Compiling a Java class using JavaCompiler" to Facebook Submit "Compiling a Java class using JavaCompiler" to Digg Submit "Compiling a Java class using JavaCompiler" to del.icio.us Submit "Compiling a Java class using JavaCompiler" to StumbleUpon Submit "Compiling a Java class using JavaCompiler" to Google

Categories
Java SE

Comments