Java compiler API, compiling made easy
1,885 viewsBefore Java 1.6 was released to the public I used the Runtime class (found in java.lang) to call the java compiler (javac.exe) directly to compile a Java file from another Java application, the line of code that i used looks like this
-
Runtime.getRuntime().exec("C:\\WINDOWS\\system32\\cmd.exe /c start javac " + userDir + "\\test\\"+className + ".java");
But luckily for me Java 1.6 introduced a handy utility that makes it possible to compile a Java application within a Java application without making direct calls to an external application. To demonstrate that I wrote a HelloWorld Java file and a CompilerConsoleDemo class that compiles the HelloWorld java file.
I wrote the Java code below to compile the above code like this
-
/*--------------------------------------------------------------------------
-
CompilerConsoleDemo class
-
*****************
-
By: HappyFace http://www.engineeringserver.com - http://www.javaforums.net
-
Contact: info [@] engineeringserver.com
-
Version: 20/August/2008
-
Last updated: 20/August/2008
-
*****************
-
Note: a demo that uses the Compiler API from Java SE 1.6
-
//----------------------------------------------------------------------*/
-
import java.io.File;
-
import java.io.FileNotFoundException;
-
import java.io.FileOutputStream;
-
import java.io.FileReader;
-
import java.io.OutputStream;
-
import java.util.Scanner;
-
-
import javax.tools.JavaCompiler;
-
import javax.tools.ToolProvider;
-
-
public class CompilerConsoleDemo{
-
String errOutput =userDir + "out.txt";
-
FileReader f;
-
Scanner sc;
-
int lines = 1;
-
String input = null;
-
-
CompilerConsoleDemo CD = new CompilerConsoleDemo(userDir + "\\HelloWorld.java");
-
CD.showCode();
-
CD.CompileCode();
-
}
-
-
this.input = input;
-
}
-
-
public void showCode(){
-
String readCode;
-
lines= 1;
-
try {
-
sc = new Scanner(f);
-
while(sc.hasNext()){
-
readCode = sc.nextLine();
-
lines++;
-
}
-
e.printStackTrace();
-
}
-
}
-
public void CompileCode(){
-
lines = 1;
-
try {
-
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
-
int result = compiler.run(null,null,err,input);
-
-
if(result == 0){
-
HelloWorld hw = ( HelloWorld ) Class.forName( "HelloWorld" ).newInstance();
-
hw.main(null);
-
-
}
-
else{
-
sc = new Scanner(f);
-
String readErr;
-
while(sc.hasNext()){
-
readErr = sc.nextLine();
-
lines++;
-
}
-
}
-
e.printStackTrace();
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
The output should look like this
Compiling:
1 public class HelloWorld {
2 public static void main(String[] args){
3 System.out.println("Hello World");
4 }
5 }
Result(s):
File successfully compiled!
Output:
Hello World
But if an error occurred this will be shown instead
Compiling:
1 public class HelloWorld {
2 public static void main(String[] args){
3 System.out.println("Hello World")
4 }
5 }
Result(s):
1 C:\Eclipseworkspace\Demo\HelloWorld.java:3: ';' expected
2 System.out.println("Hello World")
3 ^
4 1 error
It looks like a really powerful utility but I haven't looked into the JavaCompiler class that much myself so there are still things that I need to figure out about the Compiler API to make maximal use it.


(3 votes, average: 3.33 out of 5)











September 17th, 2008 at 3:48 pm
[...] will once i have the time to write a readme and license for it. However you can take a look here: Engineeringserver.com | Java compiler API, compiling made easy and here Engineeringserver.com | [ java file copy ] Java folder watching to combine both of my [...]