Compiling a Java class using JavaCompiler - II

19 April 2008

You may want to compile more than one classes from a Java class using javax.tools package. This is shown in this post.

I will use the same code as given in first part of this post. Only difference is that I will use a File array and will put the file objects referring to Java classes.

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<? extends JavaFileObject> 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).

del.icio.us:Compiling a Java class using JavaCompiler - II  digg:Compiling a Java class using JavaCompiler - II  spurl:Compiling a Java class using JavaCompiler - II  wists:Compiling a Java class using JavaCompiler - II  simpy:Compiling a Java class using JavaCompiler - II  newsvine:Compiling a Java class using JavaCompiler - II  blinklist:Compiling a Java class using JavaCompiler - II  furl:Compiling a Java class using JavaCompiler - II  reddit:Compiling a Java class using JavaCompiler - II  fark:Compiling a Java class using JavaCompiler - II  blogmarks:Compiling a Java class using JavaCompiler - II  Y!:Compiling a Java class using JavaCompiler - II  smarking:Compiling a Java class using JavaCompiler - II  magnolia:Compiling a Java class using JavaCompiler - II  segnalo:Compiling a Java class using JavaCompiler - II  gifttagging:Compiling a Java class using JavaCompiler - II

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Compiling a Java class using JavaCompiler - II

Leave a Reply