如何在Eclipse工作区中获取活动包路径 [英] How to get the active package path in eclipse workspace

查看:51
本文介绍了如何在Eclipse工作区中获取活动包路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否是正确的问题,因为我无法获取我在eclipse中选择的文件夹的绝对路径。

Im not sure whether it is the right question because i m not able to get the absolute path of the folder which i have selected in eclipse.

以下是我写的代码,谁能告诉我缺少了什么?

The following is the code i wrote , can anyone tell me what is missing ?

IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (window != null)
        {
            IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
            Object firstElement = selection.getFirstElement();
            if (firstElement instanceof IAdaptable)

            {   
                IFolder folder = (IFolder)((IAdaptable)firstElement).getAdapter(IFolder.class);
                IPath path = folder.getLocation();
                System.out.println(path);
            }
        }

提前谢谢!!!

推荐答案

尝试使用路径成为绝对路径://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/runtime/IPath.html#makeAbsolute%28%29 rel = nofollow > IPath#makeAbsolute()。您还可以使用 IPath#toFile(),然后将其设为绝对(File#getAbsolutePath())。
因此,在您的示例中,您可以编写:

Try to make your path to an absolute path using IPath#makeAbsolute(). You can also transform it to an File using IPath#toFile() and then make it absolute (File#getAbsolutePath()). So in your sample you could write:

IPath path = folder.getLocation();
System.out.println("Absolute Path:     "+path.makeAbsolute());
System.out.println("Absolute FilePath: "+path.toFile().getAbsolutePath());

这篇关于如何在Eclipse工作区中获取活动包路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆