Compiling a Java class using JavaCompiler - II
19 April 2008You 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).
Related Posts:
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