【问题标题】:Root directory of eclipse plugin projecteclipse插件项目根目录
【发布时间】:2015-05-08 22:56:16
【问题描述】:

我有一个基于 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 插件的根目录?

谢谢!

【问题讨论】:

  • 所以你打算把这个文件作为你插件的一部分?
  • 是的,我想把它作为我插件的一部分

标签: java eclipse-plugin eclipse-rcp


【解决方案1】:

使用

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文件中包含该文件。

【讨论】:

    【解决方案2】:

    两种可能:

    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?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      相关资源
      最近更新 更多