0

I write a java program in which i accessed different servers and access some files. Currently i am only accessing a linux server and executing some command. Actually this is now very specific to the linux environment. I want that when i access the server , my program will check that weather is Linux environment or windows. If it is windows environment it will execute the command or if it is Linux environment it will execute the command for Linux. How can i check the platform pragmatically.

Malik
  • 585
  • 2
  • 6
  • 13

1 Answers1

8

Use:

System.getProperty("os.name");

This will let you know which OS the JVM is running on, then you can change your program flow accordingly.

List of possible values (not necessarily exhaustive):

  • AIX
  • Digital Unix
  • FreeBSD
  • HP UX
  • Irix
  • Linux
  • Mac OS
  • Mac OS X
  • MPE/iX
  • Netware 4.11
  • OS/2
  • Solaris
  • Windows 2000
  • Windows 95
  • Windows 98
  • Windows NT
  • Windows Vista
  • Windows XP
  • Windows 7
  • Windows 8

Source: http://www.javaneverdie.com/java/java-os-name-property-values/

Jops
  • 22,535
  • 13
  • 46
  • 63
  • 1
    Jops thanks for you reply . As for as i think when i use this command this will check the enviornemnt of machine on which my application is running but the problem is my application is running on one machine and then with the help of different threads it is accessing different server remotely. Now i want to check platform of that remote server. How can i check that? – Malik Jan 30 '14 at 11:23
  • +1 for listing source. – Boris the Spider Jan 30 '14 at 11:23
  • @Malik How do you access the server? – assylias Jan 30 '14 at 11:24
  • @assylias i am accessing the server through JSch library. – Malik Jan 30 '14 at 11:26
  • I'm not familiar with JSch, but that information can be derived it seems. Check this link: https://www.mail-archive.com/jsch-users@lists.sourceforge.net/msg00371.html – Jops Jan 30 '14 at 11:27
  • Are windows 7 & 8 deliberately ommited? – Richard Tingle Jan 30 '14 at 11:34
  • @RichardTingle Apparently the link is 2009-old :) Added. Working on a Mac, so cannot validate the exact value to be returned on Win7. But pattern-wise, it will most likely be "Windows 7". Thanks. – Jops Jan 30 '14 at 11:37