我在Eclipse中打开一个带菜单打开的编辑器,但我无法获取当前选定文件的路径。有时,它会给出正确的路径,但有时会抛出空指针例外。 我正在编写下面的代码来获取选定的文件路径。如何在Eclipse插件开发中获取当前所选文件的路径
IWorkbenchPage iwPage=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
System.err.println("iwpage::"+iwPage);
ISelection selection=iwPage.getSelection();
System.err.println("selection::::testtttt"+selection.toString());
if(selection!=null && selection instanceof IStructuredSelection)
{
IStructuredSelection selectedFileSelection = (IStructuredSelection) selection;
System.out.println(selection.toString());
Object obj = selectedFileSelection.getFirstElement();
selectedFile=(IResource)obj;
System.err.println("selection::::"+selectedFile.getLocation().toString());
String html=selectedFile.getLocation().toString().replace(" ","%20");
String html_file="file:///"+html;
return html_file;
}
您是否试图在编辑器中获取该文件路径? – 2012-01-09 10:07:25
其实我想通过浏览器中的路径来设置URL。 – Eshika 2012-01-09 10:24:54