简体   繁体   English

如何在Java中获取项目路径?

[英]How to Get a Project Path in Java..?

I am working in a java plugin for Eclipse and i need get the project path from plugin Example 我在Eclipse的Java插件中工作,我需要从插件示例获取项目路径。

/home/user/workspace/project_name/ / home / user / workspace / project_name /

And i dont know how. 而且我不知道如何。

I tried this but it did not work 我尝试了这个,但是没有用

if (window != null)
        {
            IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
            Object firstElement = selection.getFirstElement();
            if (firstElement instanceof IAdaptable)
            {
                IProject project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
                IPath path = project.getFullPath();
               // System.out.println(path);
                Runtime.getRuntime().exec("zenity --warning --text '"+path+"';echo $?");
            }
        }

getFullPath() returns the path of the project relative to the workspace root. getFullPath()返回相对于工作区根的项目路径。

What you want is getLocation() which returns the full path of the project (or other resource) in the file system: 您需要的是getLocation() ,它返回文件系统中项目(或其他资源)的完整路径:

IPath path = project.getLocation();

Use the IPath toOSString() method to convert the path to a string in the correct format for the current OS: 使用IPath toOSString()方法将路径转换为当前操作系统正确格式的字符串:

String pathStr = path.toOSString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM