How do you determine 32 or 64 bit architecture of Windows using Java?
I used the command prompt (command --> wmic OS get OSArchitecture) to get the OS architecture. The following program helps get all the required parameters:
import java.io.*;
public class User {
public static void main(String[] args) throws Exception {
System.out.println("OS --> "+System.getProperty("os.name")); //OS Name such as Windows/Linux
System.out.println("JRE Architecture --> "+System.getProperty("sun.arch.data.model")+" bit."); // JRE architecture i.e 64 bit or 32 bit JRE
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c","wmic OS get OSArchitecture");
builder.redirectErrorStream(true);
Process p = builder.start();
String result = getStringFromInputStream(p.getInputStream());
if(result.contains("64"))
System.out.println("OS Architecture --> is 64 bit"); //The OS Architecture
else
System.out.println("OS Architecture --> is 32 bit");
}
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
}
(Only for Windows) Check if C:\Windows\SysWOW64 exists. if the directory exist, it is a 64 bit process. Else, it is a 32 bit process.
Maybe it 's not the best way, but it works.
All I do is get the "Enviroment Variable" which windows has configured for Program Files x86 folder. I mean Windows x64 have the folder (Program Files x86) and the x86 does not. Because a user can change the Program Files path in Enviroment Variables
, or he/she may make a directory "Program Files (x86)" in C:\, I will not use the detection of the folder but the "Enviroment Path" of "Program Files (x86)" with the variable in windows registry.
public class getSystemInfo {
static void suckOsInfo(){
// Get OS Name (Like: Windows 7, Windows 8, Windows XP, etc.)
String osVersion = System.getProperty("os.name");
System.out.print(osVersion);
String pFilesX86 = System.getenv("ProgramFiles(X86)");
if (pFilesX86 !=(null)){
// Put here the code to execute when Windows x64 are Detected
System.out.println(" 64bit");
}
else{
// Put here the code to execute when Windows x32 are Detected
System.out.println(" 32bit");
}
System.out.println("Now getSystemInfo class will EXIT");
System.exit(0);
}
}
I don't exactly trust reading the os.arch system variable. While it works if a user is running a 64bit JVM on a 64bit system. It doesn't work if the user is running a 32bit JVM on a 64 bit system.
The following code works for properly detecting Windows 64-bit operating systems. On a Windows 64 bit system the environment variable "Programfiles(x86)" will be set. It will NOT be set on a 32-bit system and java will read it as null.
boolean is64bit = false;
if (System.getProperty("os.name").contains("Windows")) {
is64bit = (System.getenv("ProgramFiles(x86)") != null);
} else {
is64bit = (System.getProperty("os.arch").indexOf("64") != -1);
}
For other operating systems like Linux or Solaris or Mac we may see this problem as well. So this isn't a complete solution. For mac you are probably safe because apple locks down the JVM to match the OS. But Linux and Solaris, etc.. they may still use a 32-bit JVM on their 64-bit system. So use this with caution.
You can use the os.arch property in system properties to find out.
Properties pr = System.getProperties();
System.out.println(pr.getProperty("os.arch"));
If you are on 32 bit, it should show i386 or something
System.getProperty("os.arch");