eclipse插件项目根目录

Root directory of eclipse plugin project

我有一个基于 eclipse 插件的 RCP 应用程序。我有一个文件,我想将它放在我的插件的根目录中并从那里访问它,因此它可以在任何平台(windows 或 linux)上使用。例如,我的插件名称是 Test 并且我有一个文件 TestFile.eap

目前我在桌面上使用它

 private String  EapName = "C:\Users\abc\Desktop\TestFile.eap";

我想把 TestFile.eap 放在 Test 的根目录中,然后从那里访问它:

File f  = new File(EapName);

如何获取我的 eclipse 插件的根目录?

谢谢!

两种可能性:

1) 该文件是静态的并作为插件的一部分部署:

URL url = new URL("platform:/plugin/my.plugin.id/the/relative/path");
InputStream inputStream = url.openConnection().getInputStream();

2) 文件是即时创建的,您需要一个地方来放置它:

IPath pluginStatePath = MyPlugin.getDefault().getStateLocation();
IPath filePath = pluginStatePath.append("my/relative/filepath");

有关这两种方法的更多信息,请参阅Where do plugins store their state?

使用

Bundle bundle = Platform.getBundle("your plugin id");

URL pluginURL = FileLocator.find(bundle, new Path("TestFile.eap"), null);

URL fileURL = FileLocator.toFileURL(pluginURL);

File file = new File(fileURL.getPath());

如果您的插件被打包为 jar(通常是这样),这会将文件从 jar 中提取到一个临时位置,以便您可以使用文件 API 访问它。

确保将文件包含在插件 build.properties 文件中。