Is there a way to find the name os using java?

Is there a way to find the name os using java?

I tried the code below, but it will look like (Linux, windows ..)

System.getProperty("os.name") 

I need to define below format

Linux - "ubuntu, mandriva ..", windows - "xp, vista ..."

Sorry for my English:-(!!!

Any idea?

+6
source share
4 answers

You can use System.getProperty() to get the following properties:

  • os.name : operating system name
  • os.arch : operating system architecture
  • os.version : operating system version

In your case, I believe that you are looking for the os.version property. javadocs for System.getProperties() contains a complete list of properties you can get.

Edit

I just tested this on Linux Mint, and it seems that getting the os.version property actually returns the kernel version, not the distribution version:

 Linux amd64 2.6.38-8-generic 

After finding this message, it seems that there is no reliable way to find which Linux distribution you are using in the Java API.

If you know that you are running Linux, you can instead run one of the following system commands with Java, but you will need to run grep / parse out:

  • cat /etc/*-release
+11
source

This may help you:

 System.out.println("\nName of the OS: " + System.getProperty("os.name")); System.out.println("Version of the OS: " + System.getProperty("os.version")); System.out.println("Architecture of the OS: " + System.getProperty("os.arch")); 

EDIT: This is what it returns on Windows:

 Name of the OS: Windows XP Version of the OS: 5.1 Architecture of the OS: x86 
+5
source

There are several system properties that you can request. See the tutorial for more details. A combination of some or all of these 3 should help you:

 System.getProperty("os.arch") System.getProperty("os.name") System.getProperty("os.version") 
+1
source

Source: https://habr.com/ru/post/914524/


All Articles