dcsimg
 

Finding the OS Architecture in Java using System.getProperty

Monday May 3rd 2021 by MS Sridhar
Article Image

Inside you will find an example of using Java code to figure out which OS architecture a system is using and how to use the System.getProperty method.

When creating a program, you may find that you need to figure out which Operating System architecture a computer is running on. Is it a 32 or 64 OS architecture, for example. Java allows you to find out the Operating System version using the System.getProperty method and then make informed decisions based on the return value.

Below you will find an example of using Java code to figure out which OS architecture a system is using and a sample of how to use the System.getProperty method:

*/

public class OsArchitecture{

	public static void main(String []args){
		OsArchitecture osArchitecture = new OsArchitecture();
		osArchitecture.proceed();
	}

	private void proceed()
	{
		String osArch = System.getProperty("os.arch");
	    System.out.println(
			osArch.indexOf("32") != -1 
			?"32 bit architecture":osArch.indexOf("64") != -1 
			?"64 bit architecture":"Can't determine");
	}
}

/*

The following output should be the result of running the Java code above, showing the result of using the System.getProperty method to find out the user’s Operating System version:

$java OsArchitecture
64 bit architecture
Home
Mobile Site | Full Site