Tuesday, July 10, 2007

Inserting JVMs into Eclipse

As one of the use cases for my Summer of Code project, an auto-configuration plugin for eclipse, I've been trying to automatically detect installed Java Runtime Engines (JREs) on a system and load them into Eclipse's list of installed JREs. It ain't easy, lemme tell ya.

I first tried to insert the JRE's data directly via the Preferences API. That didn't work because format in which they're stored in the Prefs is not API, hence there are quite a few values that have to be set *very* carefully.

I next tried to dynamically load an extension for adding JREs to the list using IExtensionRegistry.addContribution but that method wouldn't let me add persistent entries. I got an idea from Phillipe to create a plugin with an extension point which I would load from disk as needed and wala! have dynamically added JRE entries. Eventually the idea was scrapped, because there was a seemingly easier way to do it using the VMStandin class.

I tried using it for a while, even resorting to using internal APIs but I kept getting weird exceptions. Then I cam in to work this morning and after updating, I noticed that Markus had kindly gotten the VMStandin class to properly insert JREs into Eclipse's list. This was one chunk of code that I was very grateful to see. Here it is for your enjoyment:

// Take the first VM install type that claims the location as a
// valid VM install. VM install types should be smart enough to not
// claim another type's VM
for (int j = 0; j < vmTypes.length; j++) {
IVMInstallType type = vmTypes[j];
IStatus status = type.validateInstallLocation(location);
if (status.isOK()) {
VMStandin standin = new VMStandin(type, type.getId());
standin.setName(location.getName());
standin.setInstallLocation(location);
standin.convertToRealVM();
break;
}
}

In other news, H.P. Lovecraft has just reintroduced me to the joys of reading horror. I just read The Tomb and was blown away by the imaginativeness of the stories and the flowing flowery language. And I also discovered that he is the source of famous names as Sephirtoh, Chthon, Alhazred, and Arkham.

2 comments:

Markus said...

Well, I have to admit that the code originally comes from org.eclipse.jdt.internal.debug.ui.jres.InstalledJREsBlock#search(File, List, List, Set, IProgressMonitor). So thanks once again should go out to the JDT team. ;-)

Anonymous said...

See also https://bugs.eclipse.org/bugs/show_bug.cgi?id=86710