1

I need to detect OS name & version in Java. That I can do by

String os_name = System.getProperty("os.name", "");
String os_version = System.getProperty("os.version", "");

but the problem is that this is not reliable. Sometimes it returns incorrect information, and I can't detect all operating systems, except the most popular Windows, MacOS, Linux etc, and this even provides wrong information in the case of 64 bit operating systems. I need to detect any OS with any kind of specification. I am unable to find the right solution for this.

Maybe I can do this with JavaScript? If it's impossible in Java then please tell me how to do it with JavaScript.

Any input or suggestions highly appreciated.

Thanks in advance.

Best regards,

**Nilanjan Chakraborty

2
  • 8
    You don't need to give your Mobile number and Email :) Commented Nov 11, 2010 at 13:49
  • 2
    you should indeed edit your post and remove your contact data. Commented Nov 11, 2010 at 13:50

3 Answers 3

9

In Java there are some common bugs while guessing the operating system:

https://bugs.java.com/bugdatabase/view_bug?bug_id=6819886

Maybe in newer versions of Java, they are solved.

In Javascript you can use navigator.appVersion:

// This script sets OSName variable as follows:
// "Windows"    for all versions of Windows
// "MacOS"      for all versions of Macintosh OS
// "Linux"      for all versions of Linux
// "UNIX"       for all other UNIX flavors 
// "Unknown OS" indicates failure to detect the OS

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

document.write('Your OS: '+OSName);
Sign up to request clarification or add additional context in comments.

2 Comments

Of course you should be aware that the OS information can be very easily spoofed.
I think it is bug of Windows 7. If you upgrade from vista to Windows 7, it may remain as vista.
3

All the browser's I'm familiar with report the OS from the navigator object (javascript)-

alert(navigator.platform)//returns string- eg'Win64'

Comments

-2

There are not too much ways to detect this. Most probably it will work correctly 99% of the time.

However if you are looking for another method, you can consider checking some magic file paths. i.e:

  • if /etc or /proc folders exists, it is Linux
  • if C:\Windows\ exists, it is Windows.
  • etc.

However they won't be again reliable since /etc /proc may also exist in Mac.

If you want to find exact name of the OS, you can look at /etc/issue file in Unix-Linux-Mac and you can use "os.name" (which you claim that it is inaccurate) or WMI (windows management instrument) to retrieve OS name+version.

3 Comments

So I can add etc in my Windows root and BAM, I'm running Linux?
it would be c:\etc in windows not /etc. it is about delimiters and absolute paths.
You must be careful, as new File('/etc').exists() on Windows will return true if C:\etc is a directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.