打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
从插件/RCP中取得文件路径的方法 中国Eclipse社区
从插件/RCP中取得文件路径的方法

作者:hopeshared
引用地址:http://www.blogjava.net/hopeshared/archive/2005/12/20/24798.html

最近社区里问这个问题的人特别多,所以在这里将自己用到的几个方法写出来。假如以后还有其他的方法,会进行更新。

从插件中获得绝对路径:
    AaaaPlugin.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath());


通过文件得到Project:

IProject project = ((IFile)o).getProject();


通过文件得到全路径:

String path = ((IFile)o).getLocation().makeAbsolute().toFile().getAbsolutePath();



得到整个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);


得到Appliaction workspace:

Platform.asLocalURL(PRODUCT_BUNDLE.getEntry("")).getPath()).getAbsolutePath();

得到runtimeworkspace:
Platform.getInstanceLocation().getURL().getPath();

从编辑器来获得编辑文件:

IEditorPart editor = ((DefaultEditDomain)(parent.getViewer().getEditDomain())).getEditorPart();
IEditorInput input = editor.getEditorInput();
if(input instanceof IFileEditorInput){
IFile file = ((IFileEditorInput)input).getFile();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
得到项目的绝对路径:

FileLocator.toFileURL(
               Platform.getBundle(Application.PLUGIN_ID).getEntry(""))
               .getPath()

///////////////////////////////////////////////////////////////////////////////////////////////////////////
从插件中获得绝对路径:
  AaaaPlugin.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath());

getStateLocation() 报错NullPointerException!
///////////////////////////////////////////////////////////////////////////////////////////////////////////
Location installLoc = LocationManager.getInstallLocation();
    String path = null;
    String installPath = null;
    if (installLoc != null)
    {
        URL installURL = installLoc.getURL();
        // assume install URL is file: based
        path = installURL.getPath();
    }
    installPath = path.substring(1, path.length());
这个方法不错,我花了一个下午看源代码找到的。
如果你的RCP工程没有发布,得到的是eclipse的路径。如果你的RCP工程已经发布(假如在D:\RCP\下面),得到的路径就是D:/RCP/。
大家可以试试看 。
///////////////////////////////////////////////////////////////////////////////////////////////////////////

请问IEditorPart editor =((DefaultEditDomain)(parent.getViewer().getEditDomain())).getEditorPart();这一句里面的parent是什么?我直接用下面的代码取代parent.getViewer().getEditDomain()这一句可不可以?

IWorkbenchPage workbenchPage = VecasCompile.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editor = workbenchPage.getActiveEditor();

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
eclipse 路径的获取
Java 中 File.getPath() getAbsolutePath()和getCanonicalPath()的不同
Eclipse插件开发 RCP生成jar包后获取jar包中的Plugin/Bundle文件资源(路径)——以FreeMarker为例
RCP运行路径检测报告
NSBundle
java中File类getPath()、getAbsolutePath()、getCanonicalPath()区别?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服