Domanda

I have a program which gives me Architecture type as x86 on a 64bit system when I use a 32 bit java for execution.

if I provide 64 bit java then it gives back x64.

How is java bitmode affecting sigar output?

Or is there a different command which can give back proper machine arch and bitmode type?

Here is my sample code

import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class SigarInfo {
    private final Sigar sigar = new Sigar();            
    private final OperatingSystem os = OperatingSystem.getInstance();

    public static void main(String[] args) throws SigarException{
        SigarInfo sysInfo = new SigarInfo();
        System.out.println("Arch: "+sysInfo.os.getArch());
        System.out.println("Datamodel: "+sysInfo.os.getDataModel());
    }
}

Output on 64bit machine

Arch: x64
Datamodel: 64

Output on 32bit machine

Arch: x86
Datamodel: 32

Output on 64bit machine with 32 bit Java

Arch: x86
Datamodel: 32

Version used: hyperic-sigar-1.6.4

È stato utile?

Soluzione

Sigar and pure Java always return architecture of JVM, not of operating system. This is because information about your OS is useless for most of applications.

In 99.9% of cases you need to know exact architecture of your JVM process, not the OS version, because this directly affects your application. JVM architecture determine which native libraries your app should use, your memory structure, your CPU running mode, many other aspects.

Imagine that you are running x86 JVM on x64 Windows. You need to determine how much memory can be used by your application. Based on JVM info I can reply on this question. Based on OS version I can't say anything.

So using OS information may lead to incorrect decisions, so I don't recommend to use it at all. Even for just displaying it to users.

If you really need to determine OS version for some reason, you can use this for windows:

    Process process = Runtime.getRuntime().exec("CMD /C SYSTEMINFO | FINDSTR /B /C:\"System Type\"");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = bufferedReader.readLine();
    if (line.indexOf("x64") > 0)
        System.out.println("x64 architecure");
    else
        System.out.println("x86 architecure");

For linux you can use lsb_release -a command in same code to get architecture.

System can be determined like in this Article

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top