0

i have an executable which is available for windows, *nix and mac in both 32bit and 64bit. So, my java application needs to detect the operating system and bit architecture of programms which can be run to know which one to start.

i already searched for a solution to detect the bitness of the operating system, jvm or such but i really didnt find the only one function which will return either 32 or 64 on any arbitrary OS. Also the answers i found was like "i think os.arch should return this if maybe on some kind of windows box" - wtf?

Question: How can i know which application architecture will surely execute using Runtime.getRuntime().exec()?

So i'm asking for a function like this:

public int getApplicationArchitecture(){
   if(osCanRun32bit())
       return 32;
   else
       return 64;
}

One can assume that a class like below is used (copied from mkyong.com):

public class OSValidator {

private static String OS = System.getProperty("os.name").toLowerCase();

public static void main(String[] args) {
    System.out.println(OS);

    if (isWindows()) {
        System.out.println("This is Windows");
    } else if (isMac()) {
        System.out.println("This is Mac");
    } else if (isUnix()) {
        System.out.println("This is Unix or Linux");
    } else if (isSolaris()) {
        System.out.println("This is Solaris");
    } else {
        System.out.println("Your OS is not support!!");
    }
}

public static boolean isWindows() {
    return (OS.indexOf("win") >= 0);
}

public static boolean isMac() {
    return (OS.indexOf("mac") >= 0);
}

public static boolean isUnix() {
    return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 );
}

public static boolean isSolaris() {
    return (OS.indexOf("sunos") >= 0);
}

}

thx for any hint on this

John Doe
  • 2,746
  • 2
  • 35
  • 50
  • A 32-bit application will 'surely execute' on both, if it's installed. – user207421 Jul 22 '14 at 10:06
  • possible duplicate of [How to find the OS bit type](http://stackoverflow.com/questions/20856694/how-to-find-the-os-bit-type) – Typo Jul 22 '14 at 10:09
  • 1
    @EJP it's false, there's situations, when you can't run 32-bit application on 64-bit platform, for example, when you setting up 32-bit skype on 64-bit ubuntu, you get along with it much many additional libraries, needed to run it. – Dmitry Ginzburg Jul 22 '14 at 10:16
  • What's the problem of `System.getProperty("os.arch").contains("64")?64:32` ("amd64"/"i586") – Dmitry Ginzburg Jul 22 '14 at 10:16
  • @DmitryGinzburg what do you mean by amd64/i586? this values are the only one's not matching contains("64") and are 64? so your solution will work on all major systems? – John Doe Jul 22 '14 at 10:52
  • I meant, that on the modern processors that's the only possible values. – Dmitry Ginzburg Jul 22 '14 at 11:09
  • @DmitryGinzburg you know of a list on what os.arch surely will look like under which circumstances? i never found an answer to this but only experienced guesses. – John Doe Jul 22 '14 at 11:26

2 Answers2

0

Just execute shell command from JAVA that depend on OS and check it's output. For instance: * Windows: wmic os get osarchitecture * Linux like: uname -m

Stanislav
  • 9
  • 2
  • thanks, i added your uname solution, but since wmic output is bit more complicated to parse and i dont have windows and there seems to be a getenv solution ill stick which Environment properties on windows – John Doe Jul 22 '14 at 11:24
0

my current solution looks like this:

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public static int getApplicationArchitecture(){
    if(isWindows()){
        String arch = System.getenv("PROCESSOR_ARCHITECTURE");
        String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");

        if(arch!=null && arch.endsWith("64")){
            return 64;
        }
        if(wow64Arch != null && wow64Arch.endsWith("64")){
            return 64;
        }
    }
    else if(isMac() || isUnix()){
        try{
            Process p = Runtime.getRuntime().exec("uname -m");
            InputStream is = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = br.readLine();
            if(line.indexOf("64")!=-1){
                return 64;
            }
        }catch(Exception e){}
    }

    // on unkown OS
    if(System.getProperty("os.arch").equals("x86")){
        return 32;
    }
    return 64;
}

in the end i'll check for 64-bitness OS specific and if it cant be determined for sure, check os.arch property - it's kind of guessing the bitness

maybe someone could comment on it or improve

John Doe
  • 2,746
  • 2
  • 35
  • 50