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

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

问题描述

我正在使用Eclipse Plugin-in-project开发一个Eclipse插件,它将在工具栏中添加一个菜单项。

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 which 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();
}

从Eclipse运行时运行方式> Eclipse Application 它给了我正确的道路。但是,当我将插件导出为jar并将其添加到Eclipse插件文件夹中时,它没有给我正确的路径。

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.

请帮助我解决此问题!

推荐答案

使用:

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

url = FileLocator.toFileURL(url);

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

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

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天全站免登陆