sprabhakaran
sprabhakaran

Reputation: 1635

Is there any way to find os name using java?

Is there any way to find os name using java ?

I have tried below code, but it will returns looks like (Linux , windows..)

System.getProperty("os.name")

I need to detecting below format

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

sorry for my English :-( !!!

Any idea ?

Upvotes: 9

Views: 10461

Answers (4)

Mansoor Siddiqui
Mansoor Siddiqui

Reputation: 21663

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 you're looking for the os.version property. The javadocs for System.getProperties() contain a full list of properties that you can retrieve.

Edit

I just tested this in Linux Mint and it appears that the getting the os.version property actually returns the kernel version, and not the distro version:

Linux
amd64
2.6.38-8-generic

After finding this post, it seems as though there's no reliable way to find which Linux distribution you're running through the Java API.

If you know you're running in Linux, you can instead run one of the following system commands from Java, but you'll have to grep/parse out the distribution:

  • cat /etc/*-release

Upvotes: 12

brimborium
brimborium

Reputation: 9512

This might 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 under Windows:

Name of the OS: Windows XP
Version of the OS: 5.1
Architecture of the OS: x86

Upvotes: 5

serg10
serg10

Reputation: 32667

There are a few system properties that you can query. Have a look a the tutorial for 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")

Upvotes: 1

Martinecko
Martinecko

Reputation: 1929

Take a look at: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#getProperties%28%29

property: 'os.name'

Upvotes: -1

Related Questions