gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:59:27 24 4
gpt4 key购买 nike

我正在使用 Eclipse 的 java 插件,我需要从插件获取项目路径示例

/home/user/workspace/project_name/

我不知道怎么办。

我尝试过,但没有成功

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() 返回项目相对于工作空间根目录的路径。

您想要的是 getLocation() 它返回文件系统中项目(或其他资源)的完整路径:

IPath path = project.getLocation();

使用 IPath toOSString() 方法将路径转换为当前操作系统正确格式的字符串:

String pathStr = path.toOSString();

关于java - 如何在 Java 中获取项目路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36929285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com