问题描述
我正在尝试使用Java以编程方式找到当前正在运行/已调试的项目的路径,我在Google上查找到的结果是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启动.插件在Eclipse代码的工作台中添加了一个按钮,并在插件代码中当我单击按钮时,我将搜索当前目录路径.
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 pluginwhen 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)中找到当前项目目录的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!