The Java 6.0 Compiler API

by Krishna Srinivasan

One of the cool features available in Java 6.0 (Mustang) is the 'Java Compiler API'. This API is a result of the JSR (Java Specification Request) 199 which proposes that there must be a standard way to compile java source files. The result of the JSR is the new 'Java Compiler API' and one can use this new feature to compile java source files from within java files. Previously developers were depending on the low-level issues like starting a process representing the javac.exe. Though this feature is not intended to every one, Editors or IDE (Integrated Development Environment) can make much use of this new feature for compiling Java source files in a better manner. Here Shunmuga Raja explains in very detail.
The Java 6.0 Compiler API

5 Comments

Tim
2007-04-01 07:50:45
Hmm, it would be interesting to use this to essentially cache your reflection results.
inquirydog
2007-04-01 09:07:20

I was excited when I first read this because I thought it would be a way to execute snippets of java code created at runtime, but read the article and realized that it was only a way to compile full files at runtime (ie- this could have been done in the past in a system dependent way with Runtime.getRuntime()exec("javac ....")).


I am eager for java to figure out a way to execute runtime generated snippets of code! Few languages really do this, and most are implemented terribly (ie- javascript). Lisp does it well, but is very complicated to master. Java has a history of taking good yet complicated ideas and implementing them simply and cleanly. This is an area where Java could really make a difference.

Ricky
2007-04-03 12:38:52
inquirydog; have you looked at BeanShell scripting (www.beanshell.org)? You can copy/paste Java code to BeanShell and have it run as-is as interpreted code.


see also: http://jcp.org/en/jsr/detail?id=274
JSR 274: The BeanShell Scripting Language
This specification will standardize BeanShell, a Java syntax compatible scripting language for the Java platform.


adam
2007-04-03 12:57:39
Looks like they show how to make a class file from a string on page 4.

2007-08-24 12:19:16
public class dice
{


public static void main(String[] args)
{
DiceSet game = new DiceSet();
game.run();


}
}



class DiceSet
{
double rand;
int Die;
int rollAmount;



public void run()
{
rollAmount = 0;
printWelcomeMessage();

}




public void roll()
{
int count = 0;
while (count < rollAmount)


{
getRandomNumber();
rollDie();
System.out.print(Die + " ");
count++;


}
}



private void printWelcomeMessage()
{
//print out a welcome message
System.out.println("Please select a number between 1-6: ");


if (rollAmount == 0)


System.out.println("Is 0 between 1-6?!?");


else if (rollAmount > 6)


System.out.println("I said between 1-6!!");


else


roll();


}

private void getRandomNumber()
{


//generate two random numbers
//use Math.random() to give you a random number x
//x will have a value.... 1 >= x >= 0
rand = Math.random();


}


private void rollDie()
{


if(rand > .8333)


Die = 6;


else if(rand > .6666)


Die = 5;


else if(rand > .4999)


Die = 4;


else if(rand > .3333)


Die = 3;


else if(rand > .1666)


Die = 2;


else


Die = 1;


}



}