Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

The foolproof way of finding application's directory(folder) ?

843810Jun 29 2010 — edited Jun 30 2010
Hi All,

What is the foolproof way of finding one's application's directory through code?

The property "user.dir" is no good as it returns different values for the same application without any change in the application's location. I found that on the first run of the application, it returns the correct directory path, but on the second or subsequent runs, it returns the path of a subdirectory of the application.

Same goes for the method - new File().getAbsolutePath()

I googled for it and found only the above two methods are mentioned in the forums where this question has been raised. Surely there must exist a more reliable way of finding one's application's path.

Thanks for any advice that you can offer.

Regards,
Dinesh Bajaj

Comments

User_64CKJ
dineshbajaj wrote:
Re: The foolproof way of finding application's directory(folder) ?
...
State the goal or program feature you are trying to achieve, rather than the (flawed) strategy that you are currently pursuing in efforts to achieve the feature.
..Thanks for any advice that you can offer.
You're welcome.
843810
Hi Andrew,

Thanks for your response.

Let me state my goal: There are Reports and Database folders under my application folder. I try to connect to the database by specifying its path as System.getProperty("user.dir") + File.separator + "Database". Similarly, I try to access a report by specifying its path as System.getProperty("user.dir") + File.separator + "Reports" + File.separator + reportFileName.

In order to achieve the above-stated objectives, it is imperative that every single time the code returns the correct path to the application folder. However, this doesn't happen always. Say, the application is installed in the folder : c:\program files (x86)\ABC; then, the property "user.dir" is found to be retuning "c:\program files (x86)\ABC\Database", "c:\program files (x86)\Reports", or "c:\program files (x86)\ABC\ABC". This erratic behavior causes the application to fail.

Hope the problem description is clear.
User_64CKJ
dineshbajaj wrote:
..Let me state my goal: There are Reports and Database folders under my application folder. ..
How do they get there? Do you use an installer of some type? If so, get the installer to put the reports/DB folder into a sub-directory of user.home. Or better, store a String in the same location, representing the directory where the user chose to install the reports/DB, then use that as the path.
..I try to connect to the database by specifying its path as System.getProperty("user.dir") + File.separator + "Database". Similarly, I try to access a report by specifying its path as System.getProperty("user.dir") + File.separator + "Reports" + File.separator + reportFileName.

In order to achieve the above-stated objectives, it is imperative that every single time the code returns the correct path to the application folder. However, this doesn't happen always. Say, the application is installed in the folder : c:\program files (x86)\ABC; then, the property "user.dir" is found to be retuning "c:\program files (x86)\ABC\Database", "c:\program files (x86)\Reports", or "c:\program files (x86)\ABC\ABC". This erratic behavior causes the application to fail.
Or if the user drags an app. shortcut to the desktop, the path will point to the desktop. ... I could add more 'corner case' modes of failure, but I think you get the idea. The user.dir is not really good for much.
Hope the problem description is clear.
Yes. Much more clear, thanks.
843810
Many thanks for your quick response.
How do they get there? Do you use an installer of some type? If so, get the installer to put the reports/DB folder into a sub-directory of user.home. Or >>better, store a String in the same location, representing the directory where the user chose to install the reports/DB, then use that as the path.
Yes, I do use Winamp installer script to create an installer. I like your idea of putting the reports folder into a sub-directory of "user.home". I would though avoid putting database folder into that directory as it is in the same drive where the OS is installed, and that drive is often formatted by the users.

I am not too conversant with the NSIS script and so I am not confidant that I will be able to tweak the existing script to create another dialog to prompt for the database location. I instead intend to ask for this location through my application, and then creating the database in runtime there.
Or if the user drags an app. shortcut to the desktop, the path will point to the desktop. ... I could add more 'corner case' modes of failure, but I think you >>get the idea. The user.dir is not really good for much.
Agree that user.dir is not really good for much; however, it is smart enough to not point to the shortcut location as I launch the application through the desktop shortcut only and have noticed that it never points to that shortcut.

Thanks again for all your input.
1 - 4
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 28 2010
Added on Jun 29 2010
4 comments
106 views