Reputation: 429
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
Upvotes: 1
Views: 6139
Reputation: 104760
All the browser's I'm familiar with report the OS from the navigator object (javascript)-
alert(navigator.platform)//returns string- eg'Win64'
Upvotes: 3
Reputation: 45196
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:
/etc
or /proc
folders exists, it is LinuxC:\Windows\
exists, it is Windows.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.
Upvotes: -2
Reputation: 4398
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);
Upvotes: 9