java 如何使用插件在eclipse中获取项目文件的绝对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15246736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:58:34  来源:igfitidea点击:

How to get the absolute path of project files in eclipse using plugin

javaeclipse-plugineclipse-api

提问by user1901079

I am trying to create a plugin which would give me a list of absolute path of all the files inside a project opened in eclipse.

我正在尝试创建一个插件,它会给我一个在 eclipse 中打开的项目中所有文件的绝对路径列表。

I tried but I am able to get the path of the active window only..

我试过了,但我只能得到活动窗口的路径..

My action code is:

我的操作代码是:

  IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart(); 
    IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
    if (file == null)
        try {
            throw new FileNotFoundException();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    String path = file.getRawLocation().toOSString();
    System.out.println("path: " + path);

Here I am only getting the path for active window..But I want list of absolute path of all the files inside a project ..mainly the files under src folder...

在这里,我只获取活动窗口的路径..但​​我想要项目中所有文件的绝对路径列表..主要是 src 文件夹下的文件...

Please guide me if I can do it in the same way or do I need to use some different API for this.

请指导我,如果我可以用同样的方式来做,或者我需要为此使用一些不同的 API。

回答by Pradeep Simha

After my research, I found out below code would get the path of Eclipse's current workspace's project directory:

经过我的研究,我发现下面的代码将获取 Eclipse 当前工作区的项目目录的路径:

//get object which represents the workspace  
IWorkspace workspace = ResourcesPlugin.getWorkspace();  

//get location of workspace (java.io.File)  
File workspaceDirectory = workspace.getRoot().getLocation().toFile()

Note:You need to import org.eclipse.core.resourcesand org.eclipse.core.runtimeto use these API's

注意:您需要导入org.eclipse.core.resourcesorg.eclipse.core.runtime使用这些 API

Source

来源