What is the best way to find the users home directory in Java?

后端 未结 9 770
清歌不尽
清歌不尽 2020-11-22 09:12

The difficulty is that it should be cross platform. Windows 2000, XP, Vista, OSX, Linux, other unix variants. I am looking for a snippet of code that can accomplish this for

9条回答
  •  一向
    一向 (楼主)
    2020-11-22 09:45

    Actually with Java 8 the right way is to use:

    System.getProperty("user.home");
    

    The bug JDK-6519127 has been fixed and the "Incompatibilities between JDK 8 and JDK 7" section of the release notes states:

    Area: Core Libs / java.lang

    Synopsis

    The steps used to determine the user's home directory on Windows have changed to follow the Microsoft recommended approach. This change might be observable on older editions of Windows or where registry settings or environment variables are set to other directories. Nature of Incompatibility

    behavioral RFE
    
    6519127
    

    Despite the question being old I leave this for future reference.

提交回复
热议问题