In Netbeans what is the correct way to get the file path of the currently Opened Project. In the module I am developing I need to acquire the path of the Project for a FileChooser however most of what I tried simply returned the path of the module it is executing from. Is their a way to get the path of the Project that the method is run from?
Asked
Active
Viewed 8,088 times
5
-
If it helps the method that opens the File Explorer is from a subNode of the DataNode of a File Type created for the project. – kdgwill Jan 01 '12 at 03:11
-
In netbeans, probably the path will be C:\Users\your_username\Documents\NetBeansProjects :D – COD3BOY Jan 01 '12 at 03:15
-
What type of project is this? In most cases, you will not be offering a file chooser to a user to select 'application resources'. BTW - what is a `FileExplorer`? – Andrew Thompson Jan 01 '12 at 03:18
-
It is an editor for a project I am working on. It assumes their is a folder of external assets in the Project to choose from depending on whats needed. Originally I was using assets from the module itself but I realized it wouldn't be practical since their are new assets being added. I would have to add the new asets and recompile the module and then reinstall it. So i figured it would be easier for the module to assume their are multiple folders in A project which contains the assets needed for the editor. – kdgwill Jan 01 '12 at 03:42
-
@AndrewThompson I meant FileChooser, FileExplorer was the name of the method that set up all of the filters and accessories for the file chooser. – kdgwill Jan 01 '12 at 03:49
-
*"So i figured it would be easier for the module to assume their are multiple folders in A project which contains the assets needed for the editor."* Perhaps so, but that does not mean the directory containing those folders should necessarily be in the same location as the application. There are good reasons to put it in another, more easily reproducible place that is likely to be both readable and writable. E.G. a known sub-directory (e.g. based on the package name) of `user.home`. – Andrew Thompson Jan 01 '12 at 04:14
3 Answers
2
Try to get a Project instance via lookup and then
private String getProjectDirectory(final Project project) {
try {
FileObject projectDirectory = project.getProjectDirectory();
return FileUtil.toFile(projectDirectory).getAbsolutePath();
} catch (Exception e) {
//ignore the exception
return null;
}
}

Ben
- 2,235
- 18
- 17
-
Project is which class i.e., which jar gives us Project class – Shiva Komuravelly Feb 27 '13 at 11:58
-
http://bits.netbeans.org/dev/javadoc/org-netbeans-modules-projectapi/org/netbeans/api/project/Project.html – Ben Feb 27 '13 at 22:48
0
Right click on the project. Click properties. Sources tab. The "Project Folder" path is at the top.

Travis Meyers
- 23
- 8
-
This is a hard coded path it will not work for the currently opened Project that the command is being executed from if it is a completely different project from the original or if on another computer – kdgwill Jan 01 '12 at 07:09
-
-
Calling this from a module for a resource that is in a different directory from the module itself might not work either – kdgwill Jan 07 '12 at 03:00
0
Well I somewhat figured it out though I don't exactly like the fix. Since the File is in the directory of the Project its executing from and its DataObject is in the lookup of the DataNode used to create the tree structure I simply used : ((DataNode)getParentNode().getParentNode().getParentNode().getParentNode()).getDataObject().getPrimaryFile();
I'm not particulary fond of this method but it seems to work fine for now.

kdgwill
- 2,129
- 4
- 29
- 46