问题
Im not sure whether it is the right question because i m not able to get the absolute path of the folder which i have selected in eclipse.
The following is the code i wrote , can anyone tell me what is missing ?
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null)
{
IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
Object firstElement = selection.getFirstElement();
if (firstElement instanceof IAdaptable)
{
IFolder folder = (IFolder)((IAdaptable)firstElement).getAdapter(IFolder.class);
IPath path = folder.getLocation();
System.out.println(path);
}
}
thanks in advance!!!!
回答1:
Try to make your path to an absolute path using IPath#makeAbsolute(). You can also transform it to an File using IPath#toFile() and then make it absolute (File#getAbsolutePath()).
So in your sample you could write:
IPath path = folder.getLocation();
System.out.println("Absolute Path: "+path.makeAbsolute());
System.out.println("Absolute FilePath: "+path.toFile().getAbsolutePath());
回答2:
I think i got the solution........ In the above code ,when i debugged it , the IFolder was coming null.
Tried a different way and it worked .
public void run(IAction action) {
ISelection sel=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
IFolder folder=(IFolder) extractSelection(sel);
IPath path=folder.getLocation();
System.out.println("********************Folder Path"+path);
}
public IResource extractSelection(ISelection sel) {
if (!(sel instanceof IStructuredSelection))
return null;
IStructuredSelection ss = (IStructuredSelection) sel;
Object element = ss.getFirstElement();
if (element instanceof IResource)
return (IResource) element;
if (!(element instanceof IAdaptable))
return null;
IAdaptable adaptable = (IAdaptable)element;
Object adapter = adaptable.getAdapter(IResource.class);
return (IResource) adapter;
}
来源:https://stackoverflow.com/questions/17145471/how-to-get-the-active-package-path-in-eclipse-workspace