Returning the path to "My Documents" directory on Windows Platform

Posted on 2004-11-10
Medium Priority
Last Modified: 2008-03-10
In Java is it possible to get the location (path) of the "My Documents" directory in Java. This directory has the ability of being located somewhere other than it's default on C:\. For instance it can be set to be located on a corporate IT drive for auto backup purposes. So whatever function I use needs to report back the absolute path to the "My Documents" actual location. A code snippet is not necessary if you can point me to the documentation to accomplish this. Thanks.
Question by:tk1016
LVL 13

Expert Comment

ID: 12548804
try this:

String userHome = System.getProperty(:user.home");

//or user.dir please try both
LVL 13

Assisted Solution

petmagdy earned 450 total points
ID: 12548810
sorry offcourse:
String userHome = System.getProperty("user.home");

Assisted Solution

gdrnec earned 450 total points
ID: 12548814

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

TrekkyLeaper earned 600 total points
ID: 12548821
I know this works in NT-based systems for the default location, so hopefully this will give you an idea. There is a "java.home" environment variable (retrievable from System.getProperty( "java.home" )) that will point to the user's base directory. That would be c:\documents and settings\<username> in NT-based systems.

I think your best option in this case would be to pass the path to the "My Documents" directory in on the commandline:

java -Duser.document.dir="drive:\path\My Documents" my.package.MyClass

Then, you would be able retrieve it using System.getProperty( "user.document.dir" ).

I know this isn't the sure-fire answer you wanted, but I hope it will at least give you an idea.

Expert Comment

ID: 12548824
Yeah. Everyone else is right. "user.home" not "java.home".

Author Comment

ID: 12701438
Hi all,

thanks for answering...none of the above actually return the path to where "My Documents" is located if the system option to move that directory is exercised. The system can find it, but java still cannot. Another team member ended up doing some JNI so we could call the Windows Function directly.

Thanks...I have other questions though.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question