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
How to get the absolute path of project files in eclipse using plugin
提问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.resources
and org.eclipse.core.runtime
to use these API's
注意:您需要导入org.eclipse.core.resources
并org.eclipse.core.runtime
使用这些 API