原文地址:http://blog.sina.com.cn/s/blog_437ff56b0100nq6u.html
1. 得到某PLUGIN的路徑:
Platform.getBundle("mypluginid").getLocation();
eclipse採用osgi後好像還可以:
Activator.getDefault().getBundle().getLocation();
(前提是這個插件有Activator這個類.這個類繼承了ECLIPSE的Plugin類)
2. 得到工作區路徑:
Platform.getLocation();
ResourcesPlugin.getWorkspace();
Platform.getInstanceLocation()
3. 得到ECLIPSE安裝路徑:
Platform.getInstallLocation();
4. 從插件中獲得絕對路徑:
Activator.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath();
5. 通過文件得到工程Project:
IProject project = ((IFile)o).getProject();
通過文件得到全路徑:
String path = ((IFile)o).getLocation().makeAbsolute().toFile().getAbsolutePath();
6. 獲得工作空間workspace:
得到Appliaction workspace:
FileLocator.toFileURL(PRODUCT_BUNDLE.getEntry("")).getPath();
得到runtime workspace:
Platform.getInstanceLocation().getURL().getPath();
7. 得到整個Workspace的根:
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
從根來查找資源:
IResource resource = root.findMember(new Path(containerName));
從Bundle來查找資源:
Bundle bundle = Platform.getBundle(pluginId);
URL fullPathString = BundleUtility.find(bundle, filePath);
8. 從編輯器來獲得編輯文件:
IEditorPart editor = ((DefaultEditDomain)(parent.getViewer().getEditDomain())).getEditorPart();
IEditorInput input = editor.getEditorInput();
if(input instanceof IFileEditorInput){
IFile file = ((IFileEditorInput)input).getFile();
}
9. 獲取插件的絕對路徑:
FileLocator.resolve(BuildUIPlugin.getDefault().getBundle().getEntry("/")).getFile();