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

Posted on 2004-11-10
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

    try this:

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

    //or user.dir please try both
    LVL 13

    Assisted Solution

    sorry offcourse:
    String userHome = System.getProperty("user.home");
    LVL 4

    Assisted Solution


    LVL 5

    Accepted Solution

    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.
    LVL 5

    Expert Comment

    Yeah. Everyone else is right. "user.home" not "java.home".

    Author Comment

    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

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now