【问题标题】:Programmatically find eclipse installation directory以编程方式查找eclipse安装目录
【发布时间】:2011-09-28 12:08:08
【问题描述】:

在我的 Eclipse 插件 (A) 中,我需要以编程方式获取运行插件 (A) 的 eclipse.exe 的路径。

有人知道获取此路径的 API 吗?我不是在插件中寻找资源,而是在 eclipse.exe 本身。

谢谢。

【问题讨论】:

    标签: eclipse plugins path executable


    【解决方案1】:

    试试下面的代码:

    import org.eclipse.osgi.service.datalocation.Location;
    
    public <T> T getService(Class<T> clazz, String filter) {
            BundleContext context = getBundle().getBundleContext();
            ServiceTracker tracker = null;
            try{ 
                tracker = new ServiceTracker(context, context.createFilter("(&(" + Constants.OBJECTCLASS + "=" + clazz.getName()  //$NON-NLS-1$ //$NON-NLS-2$
                        + ")" + filter + ")"), null); //$NON-NLS-1$ //$NON-NLS-2$
                tracker.open();
                return (T) tracker.getService();
            } catch (InvalidSyntaxException e) {
                return null;
            } finally {
                if(tracker != null)
                    tracker.close();
            }
        }
    
    getService(Location.class, Location.INSTALL_FILTER)
    

    【讨论】:

    • 这将为您获取 osgi.install.area,这是 exe 默认所在的位置,但在某些情况下,这可能与启动器可执行文件的实际位置不同。可执行文件确实将其位置传递到 java 和 org.eclipse.equinox.launcher 使用此值设置系统属性“eclipse.launcher”。
    • 获取系统属性更简单!太好了。
    【解决方案2】:

    这是 Andrew Niefer 在上述答案中的评论,更简单:

    String eclipseExecutablePath = System.getProperty("eclipse.launcher");
    System.out.println(eclipseExecutablePath);
    

    【讨论】:

    • 我认为从命令行运行eclipse时这种方式效果不佳
    猜你喜欢
    • 2017-04-27
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    相关资源
    最近更新 更多