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.
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?
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
In Java, for NIO Path, we can use path. toAbsolutePath() to get the file path; For legacy IO File, we can use file. getAbsolutePath() to get the file path.
Right click on the project. Click properties. Sources tab. The "Project Folder" path is at the top.
Two ways
System.getProperty("user.dir");
or this
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
when you remove Class.class you'd get
/home/shark/eclipse/workspace/project/src/com/package/name/bin/
which is kinda what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With