如何从Eclipse插件获取资源路径

use*_*101 5 eclipse eclipse-plugin

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

我的插件项目依赖于一个文件,该文件位于同一个插件项目中,我想要该文件的路径.

下面是我用来获取路径的示例代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

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

请帮我解决这个问题!

gre*_*449 5

用:

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

url = FileLocator.toFileURL(url);

File file = URIUtil.toFile(URIUtil.toURI(url));
Run Code Online (Sandbox Code Playgroud)

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