简体   繁体   中英

Eclipse IFolder from absolute path

I have the need to create an IFolder in an absolute location. I usually have a class that does a "build" (or at least what I call a "build") for me in the workspace. The IFolder for the build-target-folder is returned by a method like this:

public IFolder getTargetFolder(IProject project){
    return project.getFolder("build");
}

Now I created a subclass of this for the "deployment" (into a directory with absolute identifier). This subclass contains the same functionality but the getTargetfolder routine should be like this:

@Override
public IFolder getTargetFolder(IProject project){
    IPath path = new Path("M:\\Path\\To\\My\\Deployment\\Directory\\");
    IFolder target = project.getFolder(path);
    return target;  
}

However, I run into problems and I seem to not get a handle on the folder and an exception that says the folder ( <ProjectRoot>/Path/To/My/Deployment/Directory ) does not exist. How can I specify that this should not be a relative path?

The name given to project.getFolder() must be the name of a member folder (see the JavaDoc). You cannot access a file or folder with the Resources API that lies outside of the workspace. If however the M:\\\\... path lies within a projects that is part of the workspace then you can resolve the folder through IWorkspaceRoot#getContainerForLocation()

For example:

IContainer container = project.getWorkspace().getRoot().getContainerForLocation( "M:\\..." );

The returned container can be either an IProject or an IFolder . But note that the returned container does not necessarily lies within the project .

Some more information can be found in Resources and the file system

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM