Monday, October 28, 2013

Programmatically gettting Eclipse installation path directory

You might have some properties file or some other file at your eclipse home directory and you want to read that, for this you required a eclipse home directory path.

Below is the piece of code which helps you to get that.

public class EclipseLocation {

@SuppressWarnings("unchecked")
public static <T> T getService(Class<T> clazz, String filter) {
BundleContext context = MyPlugin.getDefault().getBundle().getBundleContext();
ServiceTracker tracker = null;
try {
tracker = new ServiceTracker(context, context.createFilter("(&(" + Constants.OBJECTCLASS + "=" + clazz.getName() //$NON-NLS-1$ //$NON-NLS-2$
+ ")" + filter + ")"), null); //$NON-NLS-1$ //$NON-NLS-2$
tracker.open();
return (T) tracker.getService();
} catch (InvalidSyntaxException e) {
return null;
} finally {
if (tracker != null)
tracker.close();
}
}
}

Invocation:
EclipseLocation.getService(Location.class, Location.INSTALL_FILTER)


Output:
C:/myInstallations/Eclipse



You can also achieve the same thing using the system property 'eclipse.launcher'.

//this returns C:/myInstallations/Eclipse/eclipse.exe
String launcherLocation = System.getProperty("eclipse.launcher");  //

if (launcherLocation != null ) {
File launcherLocationFile = new File(launcherLocation);

//get the parent of eclipse.exe file->C:/myInstallations/Eclipse
String eclipseDir = launcherLocationFile.getParent();

//your piece of logical code resides here
                   }



Hope this helps.

2 comments:

  1. System.getProperty("eclipse.launcher"); always give's you null.

    ReplyDelete
  2. System.getProperty("eclipse.home.location")

    ReplyDelete