8

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 ?

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
sprabhakaran
  • 1,615
  • 5
  • 20
  • 36

4 Answers4

11

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
Community
  • 1
  • 1
Mansoor Siddiqui
  • 20,853
  • 10
  • 48
  • 67
  • 2
    In fact, `uname -a` doesn't include distribution name. (Or at least on Fedora, it doesn't.) And you should probably use "/etc/system-release" ... because (again on Fedora, at least) the "/etc/*-release" pattern matches 3 pathnames. – Stephen C Apr 30 '12 at 12:53
  • Hmm, I guess that would get ugly then because on Linux Mint, the distribution name is stored in /etc/lsb-release. I remember reading a while back to include the wildcard because the file in which the distro version is stored in is not consistent across distributions. Maybe it would be worth piping the output to `grep DISTRIB_DESCRIPTION`? Either way, I'll edit my answer -- thanks for the feedback. – Mansoor Siddiqui Apr 30 '12 at 12:57
  • Greping "/etc/*-release" for DISTRIB_DESCRIPTION doesn't work on Fedora. The file(s) all consist of just "Fedora release 16 (Verne)". – Stephen C Apr 30 '12 at 14:38
5

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
brimborium
  • 9,362
  • 9
  • 48
  • 76
1

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")
serg10
  • 31,923
  • 16
  • 73
  • 94
-1

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

property: 'os.name'

Martinecko
  • 1,719
  • 4
  • 22
  • 35