简体   繁体   English

如何获取Eclipse项目资源管理器中的完整文件路径?

[英]How to get full file path present in eclipse project explorer?

I am working on eclipse plugin. 我正在开发eclipse插件。 In this i have a file name present in a project hierarchy. 在这种情况下,我在项目层次结构中有一个文件名。 i need the full path of file abc.java present in project Test. 我需要项目Test中存在的abc.java文件的完整路径。

The file presented in path F:/Test/src/main/java/com/sung/Pre/abc.java 路径F中显示的文件:/Test/src/main/java/com/sung/Pre/abc.java

    IWorkspaceRoot rootWorkspace = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = rootWorkspace.getProject("/Test");
    file1 = project.getFile("/abc.java");

    FileEditorInput fileEditorInput = new FileEditorInput(file1);

    IWorkbench workbench = PlatformUI.getWorkbench();
    IEditorDescriptor desc = workbench.getEditorRegistry().getDefaultEditor(file1.getName());
    IWorkbenchPage page11 = workbench.getActiveWorkbenchWindow().getActivePage();

    try {

        page11.openEditor(fileEditorInput, desc.getId(),true);
    } catch (PartInitException e1) {
        e1.printStackTrace();
    }

This is searching file in /Test folder. 这是在/ Test文件夹中搜索文件。 If the file presented in the root Test folder it's able to open this file but if it's inside some folder like F:/Test/src/main/java/com/sung/Pre/abc.java than it's can not find the file. 如果该文件位于根Test文件夹中,则可以打开该文件,但如果位于F:/Test/src/main/java/com/sung/Pre/abc.java之类的某个文件夹中,则无法找到该文件。

I also tried below code but facing the same issue 我也尝试下面的代码,但面临同样的问题

    try {

        //IDE.openEditor(page11, uri, "org.eclipse.ui.ide.IDE", true);
        IDE.openEditor(page11, file1, true);
    } catch (PartInitException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

so my question is if we have a file name presented in project hierarchy so how can we get the absolute or full path of that. 所以我的问题是,如果我们在项目层次结构中提供了一个文件名,那么我们如何获得该文件名的绝对或完整路径。

Please remember that i am doing this task in eclipse plugin project 请记住,我在eclipse插件项目中正在执行此任务

You can specify a path on project.getFile : 您可以在project.getFile上指定路径:

project.getFile(new Path("src/main/java/com/sung/Pre/abc.java"));

or get the IFolder for the folder containing the file and use 或获取包含该文件的文件夹的IFolder并使用

folder.getFile(new Path("abc.java"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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