Search code examples
javapatheclipse-rcpe4

How to retrieve current workspace path in RCP 4 app?


In my RCP 4 app, how can I retrieve the workspace path of the app? I tried some but failed:

ResourcesPlugin.getWorkspace().getRoot(); //Cannot used in RCP 4 app
URL fileURL = FileLocator.find(Platform.getProduct().getDefiningBundle(),new Path("/"), null); //is not workspace
Location instanceLocation = Platform.getInstanceLocation(); // the same as getInstallLocation()
String s = System.getProperty("eclipse.workspace"); //

Please help! Thanks!


Solution

  • Platform.getInstanceLocation() should work but you will have to add code in the startup to set a location if one is not set by specifying the -data command line option.

    Something like:

    Location instanceLoc = Platform.getInstanceLocation();
    // TODO check for null
    
    if (!instanceLoc.isSet()) {
      URL url = .... work out a location for the workpace 
    
      instanceLoc.set(url, false);
    }
    

    I run code like this in the @PostContextCreate method of the LifeCycle class.

    Note: Do not call the Location.getURL method until you have set the location as described above. Calling getURL before doing this will set a default of the install location (which is what you testing is showing).