【问题标题】:How to get the absolute path of project files in eclipse using plugin如何使用插件在eclipse中获取项目文件的绝对路径
【发布时间】:2013-02-21 04:59:21
【问题描述】:

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

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

我的操作代码是:

  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);

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

请指导我是否可以以相同的方式执行此操作,或者我是否需要为此使用一些不同的 API。

【问题讨论】:

标签: java eclipse-plugin eclipse-api


【解决方案1】:

经过我的研究,我发现下面的代码会得到 Eclipse 当前工作空间的项目目录的路径:

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

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

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

Source

【讨论】:

  • thanx for the inception dude..但我想访问 src 文件夹中的所有文件并获取它们的绝对路径..我可以通过某种方式遍历工作区目录...
  • 这对我不起作用,我的项目可能不在工作区中
【解决方案2】:

给定IResource,您可以使用返回IPath 的方法org.eclipse.core.resources.IResource.getLocation(),并带有“此资源在本地文件系统中的绝对路径,如果无法确定路径,则返回null”。然后你可以使用返回字符串表示的方法org.eclipse.core.runtime.IPath.toOSString()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多