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

2

6

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.

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...

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

Pastoral answered 6/3, 2013 at 11:57 Comment(2)
+1, Even I was looking for the same, and was about to post in SO :)Narcotize
Try this to iterate through files and folders : https://mcmap.net/q/1776870/-recursively-list-all-files-in-eclipse-workspace-programmaticallySubmaxillary
N
7

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

//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

Source

Narcotize answered 6/3, 2013 at 12:1 Comment(2)
thanx for the inception dude..but I want to access all the files in the src folder and get their absolute paths..can i iterate through the workspace directory by some means...Pastoral
It's not worked for me, my project probably not in workspaceKodiak
G
0

Given an IResource, you can use the method org.eclipse.core.resources.IResource.getLocation() that returns an IPath with the "absolute path of this resource in the local file system, or null if no path can be determined". Then you can use the method org.eclipse.core.runtime.IPath.toOSString() that returns a String representation.

Gracchus answered 13/7, 2021 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.