User:Higgs1/Minecraft App Dir

The .minecraft directory contains all the necessary files to run minecraft and all of the user generated files such as save games. The location of this folder varies per platform as follows. The "os" variable is generated by  in Java. The exact values returned are entirely up to the specific Java implementation in use, but are usually very reliable (for Windows & Linux at least). The string "{user.home}" represents the Java system property "user.home".

The value of the Java system property "user.home" is again entirely up to the implementation, and in fact in Sun/Oracle's implementation this is sometimes incorrect. In Linux systems this value is generally set by the PAM, and not $HOME.

For more information see the .minecraft page.

Java
The following code is 100% compatible. As in, this is exactly how the official minecraft client does it.

import java.io.File;

static final File getMinecraftWorkingDir { String home = System.getProperty("user.home", "."); String os = System.getProperty("os.name").toLowerCase; if (os.contains("solaris") || os.contains("sunos")        || os.contains("linux") || os.contains("unix")) return new File(home, ".minecraft"); else if (os.contains("win")) { String appdata = System.getenv("APPDATA"); return new File(appdata != null ? appdata : home, ".minecraft"); } else if (os.contains("mac")) return new File(home, "Library/Application Support/minecraft"); return new File(home, "minecraft"); }

Alternate Version
This is mostly a code golf version. This will emulate the official minecraft client exactly, unless the user is on a posix machine and has set the "APPDATA" environment variable to a directory other than their home.

import java.io.File;

static final File getMinecraftWorkingDir { String str = (str = System.getenv("APPDATA")) == null ? System .getProperty("user.home", ".") : str; return new File(str, ((str = System.getProperty("os.name")        .toLowerCase).matches(".*(win|s(olari|uno)s|linux|unix).*") ? "."        : str.contains("mac") ? "Library/Application Support/" : "")        + "minecraft"); }