Where am I? Operating System detection with Java
Cross-platform or not, sometimes it’s important to know on which operating system your program is running on (because you need to know where your jar file is, for example).
Maybe not the nicest way to do it, but working:
private static final String MAC_ID = "Mac";
private static final String WINDOWS_ID = "Windows";
private String OSIdentifier;
private boolean isMac;
private boolean isWindows;
private boolean isUnix;
private String dataPath;
private String configPath;
private String jarPath;
private void init()
{
OSIdentifier = System.getProperty("os.name");
if (OSIdentifier.indexOf(MAC_ID) > -1)
{
isMac = true;
dataPath = System.getProperty("user.home") + "/Library/Application Support/myProject/";
jarPath = "./Contents/Resources/Java/";
}
else if (OSIdentifier.indexOf(WINDOWS_ID) > -1)
{
isWindows = true;
dataPath = System.getProperty("user.home") + "\\myProject\\";
jarPath = ".\\";
}
else
{
isUnix = true;
dataPath = System.getProperty("user.home") + "/.myProject/";
jarPath = "./";
}
}
Add getters and setters, and you’re good to go. Of course, you might further differentiate between different Unix systems according to your needs.