What is the cross-platform way of getting the local application data directory path?

I need a platform-independent way to get the path to the data directory of a local application. System.getenv("LOCALAPPDATA") seems to work only with Windows. How should I do it?

+38
java cross-platform
Jun 20 '12 at 6:29
source share
7 answers

Perhaps you could say something like (contradict me if I am wrong, or if this is a bad approach)

 private String workingDirectory; //here, we assign the name of the OS, according to Java, to a variable... private String OS = (System.getProperty("os.name")).toUpperCase(); //to determine what the workingDirectory is. //if it is some version of Windows if (OS.contains("WIN")) { //it is simply the location of the "AppData" folder workingDirectory = System.getenv("AppData"); } //Otherwise, we assume Linux or Mac else { //in either case, we would start in the user home directory workingDirectory = System.getProperty("user.home"); //if we are on a Mac, we are not done, we look for "Application Support" workingDirectory += "/Library/Application Support"; } //we are now free to set the workingDirectory to the subdirectory that is our //folder. 

Note that in this code I fully understand that Java treats '/' the same as '\\' when working with directories. Windows uses '\\' as pathSeparator, but is also happy with '/' . (At least Windows 7 is.) It also does not case-sensitive environment variables; we could just as easily say workingDirectory = System.getenv("APPDATA"); and it would work just as well.

+28
May 21 '13 at
source share

For moderate amounts of data, consider the java.util.prefs.Preferences mentioned here or the javax.jnlp.PersistenceService discussed here . Both are cross-platform.

+9
Jun 20 2018-12-12T00
source share

The quests are old, but I don’t have an answer that lists the environment clusters, and not some ridiculous absolute paths. I don't know anything about OSX. this post only contains information about Windows and Linux.

I do not have enough points to expand an existing answer, so I have to write a new one.

Linux: As mentioned earlier, there is something like freedesktop.org that defines a standard that Linux distributions try to run. There is also a subpage that defines environment variables and their default values ​​(if they are not set, they are empty by default. The application must match the tp variable by default). Link to this page: freedesktop.org env vars

Vary defined for this issue:

  • $ XDG_DATA_HOME (local) (default: $ HOME / .local / share )
  • $ XDG_CONFIG_HOME (local) (default: $ HOME / .config )
  • $ XDG_DATA_DIRS (global) (default: / usr / local / share / or / usr / share / )
  • $ XDG_CONFIG_DIRS (global) (default: / etc / xdg )

Windows XP:

  • % APPDATA% (default: C: \ Documents and Settings {username} \ Application Data strong>)

  • % CommonProgramFiles% (default: C: \ Program Files \ Common Files ) (shared program files)

  • % CommonProgramFiles (x86)% (default: C: \ Program Files (x86) \ Common Files ) (64-bit only!) (Shared program files)

  • % ProgramFiles% (default: % SystemDrive% \ Program Files )

  • % ProgramFiles (x86)% (default: % SystemDrive% \ Program Files (x86) (64-bit only)) (64-bit!)

Windows Vista +:

  • % APPDATA% (default: C: \ Users {username} \ AppData \ Roaming ) (shared access between linked workstations, local user. Save files and configs)
  • % LOCALAPPDATA% (default: C: \ Users {username} \ AppData \ Local ) (local user. Save files and configs)
  • % CommonProgramFiles% (default: C: \ Program Files \ Common Files ) (shared program files)
  • % CommonProgramFiles (x86)% (default: C: \ Program Files (x86) \ Common Files ) (64-bit only!) (Shared program files)

  • % ProgramFiles% (default: % SystemDrive% \ Program Files ) (Static data that will not be changed after installation)

  • % ProgramFiles (x86)% (default: % SystemDrive% \ Program Files (x86) (64-bit only)) (64-bit!) (Static data that will not change after installation)

  • % ProgramData% (default: % SystemDrive% \ ProgramData strong>) (mutable data affecting all users)

In short: Linux has two environment variables that cannot be set (one for configurations, one for files). On Windows so far, I can only specify one var environment for configurations and files. Use them instead of absolute paths.

+5
Oct 05 '16 at 8:27
source share

Personally, I have found appdirs to be very useful for similar use cases. It has functions that define various types of useful directories:

  • getUserDataDir
  • getUserConfigDir
  • getUserCacheDir
  • getUserLogDir
  • getSiteDataDir ← looks the way you need it
  • getSiteConfigDir

The positions that it returns are more or less standard:

+3
Jan 11 '17 at 19:13
source share

The problem is that other operating systems do not even have a clear concept of an “application data directory”. This is usually a hidden subdirectory in the user's home directory with a common name, which may or may not be the name of the application.




Mechanical snail comments:

Not true. Linux has one (~ / .local by default), and I believe OS X also does.

Firstly, it is not ~/.local . This is ~/.local/share . (Or at least this is on my Linux machine).

Secondly, this is a new idea. It looks like this comes from the people of "freedesktop.org" using the XDG Base Directory Specification . It is not mentioned in other more widely accepted specifications of how Linux / UNIX file systems should be organized. And pay attention to what they say about their “standards” on this page: http://www.freedesktop.org/wiki/

Finally, this idea is not implemented by most Linux commands. This is pretty unscientific, but looking at hidden directories on my Linux box, I can see signs of at least 40 different applications using ~/ or a custom subdirectory. In contrast, in ~/.local/share there are signs of only 16 applications.

A naming convention, implemented by less than 1/3 of applications, is hardly a “well-defined concept” ... and, of course, not in such a way that an arbitrary catalog of application data can be found in a portable way.

0
Jun 20 2018-12-12T00:
source share

There is no cross-platform way for this, because the concepts that using different OSs are too different from the "abstract" ones. I am not familiar with the * nix and Mac conventions, but there is no "home folder" on Windows, and the application should indicate whether it wants to store the material in the roaming profile ( C:\Users\<username>\AppData\Roaming\<application vendor>\<application name>\ by default) or a local profile ( C:\Users\<username>\AppData\Local\<application vendor>\<application name>\ by default).

Please note that you cannot hard-code these paths, because with a network installation, they can be somewhere else. You should not rely on environment variables because they can be changed by the user. Your application should call the SHGetKnownFolderPath function in the Windows API.

The difference between the two is that the local profile is specific for the user and the machine, while the roaming profile is user-specific, therefore, in settings similar to my university, applications placed in the roaming profile are downloaded to the server and synchronized with any computer which I enter.

Applications should indicate whether the settings they want to store are local or roaming. Unfortunately, Java does not allow applications to solve this. Instead, there is a global user-configurable parameter that determines which folder you will receive.

0
Feb 26 '16 at 11:51
source share

You can use this

 String currentDir = new File(".").getAbsolutePath(); 

or that:

 System.getProperty("user.dir") 

I prefer the first option

Hello

-2
Jun 20 '12 at 6:45
source share



All Articles