如何从eclipse插件获取资源路径 [英] How to get resources path from a eclipse plugin

查看:145
本文介绍了如何从eclipse插件获取资源路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Eclipse Plugin-in-project开发一个Eclipse插件,它将在工具栏中添加一个菜单项。我的插件项目取决于一个文件,它位于同一个插件项目中,我想要该文件的路径。
以下是我用来获取路径的示例代码:

 捆绑包= Platform.getBundle(com.feat.eclipse.plugin); 
URL fileURL = bundle.getEntry(webspy / lib / file.txt);
文件文件= null;
String path = null;
try {
file = new File(FileLocator.resolve(fileURL).toURI());
path = file.getAbsolutePath();
} catch(URISyntaxException e1){
e1.printStackTrace();
} catch(IOException e1){
e1.printStackTrace();
}

从eclipse运行时运行为 - > Eclipse应用程序,它给我正确的路径但是当我导出我的插件作为jar并添加到我的eclipse插件文件夹,它不给我正确的路径。请帮助我解决这个问题。

解决方案

使用:

  URL url = FileLocator.find(bundle,new Path(webspy / lib / file.txt),null); 

url = FileLocator.toFileURL(url);

文件文件= URIUtil.toFile(URIUtil.toURI(url));

当您的文件打包在一个jar中 FileLocator.toFileURL 将它们复制到临时位置,以便您可以使用文件访问它们。


I am developing an Eclipse plugin using Eclipse Plugin-in-project where it will add a menu item in the toolbar. My plugin project is depending on one file it is located in the same plugin project I want the path of that file. Below is the sample code I have used to get the path:

Bundle bundle = Platform.getBundle("com.feat.eclipse.plugin");
URL fileURL = bundle.getEntry("webspy/lib/file.txt");
File file = null;
String path=null;
try {
    file = new File(FileLocator.resolve(fileURL).toURI());
    path = file.getAbsolutePath();
} catch (URISyntaxException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}

While running from eclipse Run as--> Eclipse Application it is giving me the correct path. But when I export my plugin as jar and add it in my eclipse plugins folder its not giving me the correct path. Please help me to resolve this.

解决方案

Use:

URL url = FileLocator.find(bundle, new Path("webspy/lib/file.txt"), null);

url = FileLocator.toFileURL(url);

File file = URIUtil.toFile(URIUtil.toURI(url));

When your files are packed in a jar FileLocator.toFileURL will copy them to a temporary location so that you can access them using File.

这篇关于如何从eclipse插件获取资源路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆