Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to locate the Path of the current project directory in Java (IDE)?

Tags:

java

path

eclipse

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

like image 569
user1765376 Avatar asked Oct 22 '12 12:10

user1765376


People also ask

How do you get the path of the file in the file Java?

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.

Where is project path in Netbeans?

Right click on the project. Click properties. Sources tab. The "Project Folder" path is at the top.


1 Answers

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.

like image 73
Shark Avatar answered Oct 09 '22 22:10

Shark