【问题标题】:How to use relative path of file in eclipse plugin project?如何在eclipse插件项目中使用文件的相对路径?
【发布时间】:2015-04-10 06:48:32
【问题描述】:

我想在我的 eclipse 插件项目中找到一个文件的相对路径。

需要知道插件项目所在的Eclipse的Workspace,而不是eclipse插件运行时Application的工作空间。

【问题讨论】:

    标签: java eclipse eclipse-plugin relative-path


    【解决方案1】:
    public void getCurrentWorkspaceLocation() {
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        URL url = Nameofyourcurrentclass.class.getProtectionDomain().getCodeSource()
                .getLocation();
        System.out.println(url);
        java.io.File workspaceDirectory = workspace.getRoot().getLocation()
                .toFile();
        System.out.println(workspaceDirectory.getAbsolutePath());
    }
    

    输出:

    file:/Users/amanpreetkaur/Downloads/eclipse/../../Documents/workspace_old/SpreadSheetUpdater/
    
    /Users/amanpreetkaur/Documents/runtime-EclipseApplication
    

    首先是插件项目所在工作区的路径。 二是运行插件启动Eclipse的路径。

    【讨论】:

      【解决方案2】:
      public static InputStream getFileStream(IPath pluginRelativeFilePathToGet) {
          URL fileURL = yourPlugin.getBundle()
                          .getResource(pluginRelativeFilePathToGet.toOSString());
          URLConnection connection = fileURL.openConnection();
          return connection.getInputStream();
      } 
      

      这将返回插件相关文件的 InputStream。 您可以轻松地从此流创建文件。

      问候 萨尔吉斯

      【讨论】:

        猜你喜欢
        • 2013-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-01
        • 1970-01-01
        • 2017-01-05
        • 2021-11-11
        • 2017-03-17
        相关资源
        最近更新 更多