2

I used the answer of the following question to determine the systems bit version, which works fine besides on mac osx: How can I check the bitness of my OS using Java?? (J2SE, not os.arch)

String arch = System.getenv("PROCESSOR_ARCHITECTURE");
String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");

String realArch = arch.endsWith("64")
                  || wow64Arch != null && wow64Arch.endsWith("64")
                      ? "64" : "32";

The last line (realArch) gives me a NPE on mac, do you have any idea how I could solve it, that I get the right bit version on mac, too?

UPDATE:

I read the answer wrong, sorry for that. It works fine on windows, mac osx and ubuntu with this little change:

    String realArch = System.getProperty("os.arch").endsWith("64")
            ? "64" : "32";

    if (System.getProperty("os.name").startsWith("Windows")) {
        String arch = System.getenv("PROCESSOR_ARCHITECTURE");
        String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");
        realArch = arch.endsWith("64")
                || wow64Arch != null && wow64Arch.endsWith("64")
                ? "64" : "32";
    }
Community
  • 1
  • 1
user2693017
  • 1,750
  • 6
  • 24
  • 50

2 Answers2

2

You are not checking arch for null:

Try this:

String realArch = arch != null && arch.endsWith("64") || wow64Arch != null && wow64Arch.endsWith("64") ? "64" : "32";
Lexandro
  • 765
  • 5
  • 14
1

The environment variables you are using are OS-dependent, so of course they will not work across all platforms. Try the following for OS X:

public class Test {

    public static void main(String[] args) {
        System.out.println("Is 64Bit? " + is64BitMacOS());
    }



    public static boolean is64BitMacOS() {
        java.io.BufferedReader input = null;
        try {
            String line;
            Process proc = Runtime.getRuntime().exec("sysctl hw");
            input = new java.io.BufferedReader(new java.io.InputStreamReader(proc.getInputStream()));
            while ((line = input.readLine()) != null) {
                if (line.length() > 0) {
                    if ((line.indexOf("cpu64bit_capable") != -1) && (line.trim().endsWith("1"))) {
                        return true;
                    }
                }
            }
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        } finally {
            try {
                input.close();
            } catch (Exception ex) {
                System.err.println(ex.getMessage());
            }
        }

        return false;
    }
}
martinez314
  • 12,162
  • 5
  • 36
  • 63
  • I think I read the answer of the other question wrong. Do you know if System.getProperty("os.arch") works fine for all systems besides windows? – user2693017 Jun 17 '14 at 13:43
  • 1
    @user2693017 `os.arch` gives you the bitness of the _JVM_ not the OS. If you want to know whether Java is 64-bit, then yes, use `os.arch`. If you need to know the bitness of the underlying OS (unlikely, but maybe if say you are using some native libraries), then use the code posted in your answer and mine. – martinez314 Jun 17 '14 at 13:45
  • @user2693017 And `os.arch` works for **ALL** platforms, including Windows. – martinez314 Jun 17 '14 at 13:46
  • 1
    windows returns 32 if the program runs with 32bit java, in order to avoid compatibility issues – user2693017 Jun 17 '14 at 14:01