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;

Object cpKey=cpKeyEnum.nextElement();

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.

Changing runtime classPath of an Eclipse plugin programmatically??

While I was developing some functionality for a Eclipse plugin I came across the need to change the classpath at runtime so that some runtime generated class files can be accessed by the code. I found a hack after some Googling. ( It worked fine when run as a standalone Java program. But when tried to port my code to the plugin it failed miserbly.
Only other thing that I could think of was using a seperate classloader to load these classes which actually worked. Seems that's the way to go to be on the safe side.