eclipse插件项目的根目录

时间:2022-03-19 10:39:04

I have an RCP application based on eclipse plugins. I have a file and I want to put it inside the root directory of my plugin and access it from there, so it is available on any platform (windows or linux). For example, my plugin name is Test and I have a file TestFile.eap

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

Currently I am using it form desktop

目前我在桌面上使用它

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

I want to put TestFile.eap inside the root directory of Test and access it from there like:

我想将TestFile.eap放在Test的根目录中并从那里访问它,如:

File f  = new File(EapName);

How can I get the root of my eclipse plugin?

我怎样才能获得我的eclipse插件的根目录?

Thanks!

2 个解决方案

#1


Use

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

If your plugin is packaged as a jar (as they usually are) this will extract the file from the jar to a temporary location so that you can access it using File APIs.

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

Be sure to include the file in the plugin build.properties file.

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

#2


Two possibilities:

1) The file is static and deployed as part of your plugin:

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

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

2) The file is created on the fly and you need a place to put it:

2)文件是动态创建的,你需要一个放置它的地方:

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

For more information on both approaches, see Where do plugins store their state?

有关这两种方法的更多信息,请参阅插件在哪里存储其状态?

#1


Use

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

If your plugin is packaged as a jar (as they usually are) this will extract the file from the jar to a temporary location so that you can access it using File APIs.

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

Be sure to include the file in the plugin build.properties file.

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

#2


Two possibilities:

1) The file is static and deployed as part of your plugin:

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

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

2) The file is created on the fly and you need a place to put it:

2)文件是动态创建的,你需要一个放置它的地方:

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

For more information on both approaches, see Where do plugins store their state?

有关这两种方法的更多信息,请参阅插件在哪里存储其状态?