Friday 13 February 2009

Using Eclipse BatchCompiler

Using the BatchCompiler to compile a Java source at runtime is well documented in the Eclipse help system. The problem that I came across was providing the classpath for the compilation to happen. Actually the jars needed, I had included in the runtime classpath of my plugin and were inside the plugin project. First challenge was to get the classpath entries so that I can append them to compile String to be fed in to the compiler. Surprisingly it wasn't easy enough as I had thought. After some experimentations on plugin API I finally got following code to work.

Dictionary manifest=Activator.getDefault().getBundle().getHeaders();
Enumeration cpKeyEnum=manifest.keys();
String classpath;

while(cpKeyEnum.hasMoreElements()){
Object cpKey=cpKeyEnum.nextElement();
if(cpKey.toString().equals("Bundle-ClassPath")){
classPath=manifest.get(cpKey).toString();
break;
}
}

Activator is the plugin activator class. Here the classpath is obtained as a comma seperated list of relative paths included in the "Bundle-ClassPath" header of MANIFEST.MF.

After solving that soon I realised the BatchCompiler itself cannot access the jars from the relative paths I got from the earlier exercise. So I had to extract these jars to a temporary location and feed the absolute path of this location to the BatchCompiler.

No comments: