Search code examples
eclipsepatheclipse-emfemf

Get relative path from absolute URI for EObject


I have an URI for an EObject, which looks like this:

file:/C:/runtime-run123.product/Project1/examples/Apple.ecore#/

I get the URI by calling EcoreUtil.getURI(ePackage), where ePackage is an instance of the class EPackage.

How can I get the relative path from that URI? The result should be: /Project1/examples/Apple.ecore


Solution

  • You can use IFile#getFullPath to get relative path. You need to convert from EObject to IFile first.

    Example:

    URI uri = EcoreUtil.getURI(eObject);
    IPath path = new Path(uri.toFileString());
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
    
    System.out.println(file.getFullPath()); // Relative path