Eclipse plugin: getting IFile from string

I am doing serialization, which also contains the IFile path that needs to be saved as a string.

I am using this IFile in a plugin project. To debug or start, Eclipse launches a new workspace. This test workspace has a root somewhere relative to the plugin folder. My problem is that when I turn my IFile into an absolute path, my Eclipse test space treats the file as outside the workspace and throws exceptions. If I use the relative path of the project, creating the IFile from the string fails, and the IFile is null.

I really want to believe that it works the way I need it, but I really would like to see it. Is there a way to restore a valid IFile from a relative project path?

I am currently doing a reconstruction from String-> IFile as follows:

//name is a string with the absolute path IPath location = new Path(name); IFile file = project.getFile(location.lastSegment()); 

But, as already mentioned, it only works with an absolute path that does not work in the eclipse test space.

Thanks for the tip

+4
source share
1 answer

You are very close. Do you want to:

 IPath path = new Path(name); IFile file = project.getFile(path); 

The name must be absolute (to the workspace) or relative path to the project. This interface is defined in IContainer.getFile (IPath). I changed the variable from "location" because location usually means the actual (local) path to the OS file. To use this path:

 IPath path = file.getProjectRelativePath(); 
+5
source

Source: https://habr.com/ru/post/1381802/


All Articles