如何在Java(IDE)中定位当前项目目录的Path? [英] How to locate the Path of the current project directory in Java (IDE)?

查看:39
本文介绍了如何在Java(IDE)中定位当前项目目录的Path?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Java 中以编程方式定位当前正在运行/调试的项目的路径,我查看了谷歌,我发现的是 System.getProperty("user.id"),它没有不要让我知道项目的路径.

I am trying to locate the path of the current running/debugged project programmatically in Java, I looked in Google and what I found was System.getProperty("user.id"), which didn't get me the project's path.

我知道 C# 中的命令 Environment.currentDirectory 给出了当前运行/调试项目的路径,所以我相信 Java 中也一定有类似的方法.

I know the command Environment.currentDirectory in C# gives the path of the current running/debugged project,so I am sure there must be a similar way in Java, too.

所以我问是否有人可以告诉我或给我一个代码来如何以编程方式定位当前正在运行/调试的项目的路径?

So I am asking if anyone could tell me or give me a code to how locate the path of the currently running/debugged project programmatically?

编辑:我正在编写随 eclipse start 加载的插件.插件在eclipse的workbench中添加了一个按钮,在插件的代码中当我单击按钮时,我会搜索当前目录路径.

edit: i am writing plugin which loads with eclipse start. the plugin adds a button to the workbench of eclipse, with in the code of the plugin when i click the button i search for the current directory path.

我的目的是,例如当我调试一个项目然后单击按钮时,会弹出一个窗口,显示已调试项目的路径,谢谢

my purpose is that when for example i debug a project and then click the button, a window will pop up presenting the debugged project's path, thanks

推荐答案

两种方式

System.getProperty("user.dir");

或者这个

File currentDirFile = new File(".");
String helper = currentDirFile.getAbsolutePath();
String currentDir = helper.substring(0, helper.length() - currentDirFile.getCanonicalPath().length());//this line may need a try-catch block

这个想法是用."获取当前文件夹,然后获取它的绝对位置并从中删除文件名,所以像

The idea is to get the current folder with ".", and then fetch the absolute position to it and remove the filename from it, so from something like

/home/shark/eclipse/workspace/project/src/com/package/name/bin/Class.class

当你删除 Class.class 你会得到

when you remove Class.class you'd get

/home/shark/eclipse/workspace/project/src/com/package/name/bin/

这正是你想要的.

这篇关于如何在Java(IDE)中定位当前项目目录的Path?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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