Question

I was making a Java game that stores options and the LWJGL jars in the standard application data storage directory (I think that's C:\Users\user\AppData\roaming\application on Windows, ~/.application on Linux, and ~/Library/Preferences on Mac). Currently I just use System.getProperty(os.name) and have an if-else to select the right file path (which uses system.getProperty(user.home) and appends the correct file path to it). However, this might not work on some weird OSs because the if-else won't find Windows, Mac, or Linux in the OS name, and this also seems to be a very bad way to do this in general (also the if-else assumes the OS is Linux if it isn't Windows or Mac). I tried decompiling Minecraft, which stores the app data and the LWJGL jars in the correct path, but the code is obfuscated. Is there any better way for me to do this, or should I stick with an if-else on os.name?

Was it helpful?

Solution 2

For configuration preferences, there is the Java Preferences API.

For other files, I have not found a solution to this. The best solution, at present, is probably to dispatch on the OS type with a fallback based on the user.home property.

On Linux/Unix (not Mac), you might want to honor the XDG Base Directory specification.

OTHER TIPS

Java Preferences API was created specifically for such purpose.

Shouldn't a path like this work everywhere, i.e. without the if-else-if:

String userHome = System.getProperty("user.home");
if (!userHome.endsWith("/")) {
  userHome += "/";
}

String targetPath = userHome + ".myapp/settings.properties";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top