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
In Java there are some common bugs while guessing the operating system:
http://bugs.sun.com/view_bug.do?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);
All the browser's I'm familiar with report the OS from the navigator object (javascript)-
alert(navigator.platform)//returns string- eg'Win64'
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.
Related
I have a java system tray application that runs on both windows and mc osx.
I want to add my app to start up programs for both od those os-
which is best:
Check in code which os i'm on and then do a certain action accordingly?
Create a different installer for each os that will add the program to start up?
Also, is there any good example on the best Generic way adding program to windows(for all windows types??) by code or by install, and also a good example for creating a launch agent for the mac?
Thanks
Lior
Update
Initially i would go for #1, as it easier to manage other than managing installation packages for long. but on the other hand, it is not generic, and id like to keep my code generic as possible. And third, i'm on a very short schedule, if there is a 3rd party installers that can warp this up and create different os installer with start up, thats better, under the circumstance of course.
Since you are doing a multi platform Java application I advise you to follow it's 'Write once - run everywhere' rule and either create an self-executable jar file (both Mac and Windows are capable of running those, e.g. explained here) or you could try an out-of-the-box solution like IzPack
For #1: Check the system property os.name on a win 7 box you will get Windows 7. You can query system properties with
System.getProperty("os.name");
I would like to know whether or not Java is installed and where (path).
Perhaps it sounds strange, but my aim is to let BOINC (coded in C++) check the Java installation and then start my Java app. But therefore I need to know if BOINC can start Java natively, or if I have to also send the JRE and then start my app with this not installed JRE.
So is there a way to check the installation first?
thank you in advance!
Andreas
I would start with checking of java's environment variables,
also, in linux/unix distros you can try the "which java" command,
and in windows operating systems you can check the registry.
if all else fails, you can also try to find the java binary but I think this isn't practical (time consuming).
I have some questions about the registry.
We have
Preferences p = Preferences.userRoot();
If we execute
p.nodeExists("/HKEY_CURRENT_USER/Software/Policies/Microsoft")
it will return true.
After it:
p = p.node("/HKEY_CURRENT_USER/Software/Policies");
for(String s : p.childrenNames()){
System.out.println(">" + s);
}
We see that it has one child: "Windows". But
p.nodeExists("/HKEY_CURRENT_USER/Software/Policies/Microsoft/Windows")
returns false. Why?
Thanks.
UPDATE
Ok. I have some mistakes. Let me try again: Why does
p.nodeExists("/HKEY_CURRENT_USER/Software/Policies/Microsoft/Windows")
return false?
If you execute the code lines shown, in the order shown, when you get to the line
p.nodeExists("/HKEY_CURRENT_USER/Software/Policies/Microsoft/Windows")
p does not anymore point to the user root, but to "/HKEY_CURRENT_USER/Software/Policies".
Btw you have a probable omission in your third code sample:
p = p.node("/HKEY_CURRENT_USER/Software/Policies");
should be
p = p.node("/HKEY_CURRENT_USER/Software/Policies/Microsoft");
I stumbled on this one today. The answer you've accepted is completely wrong.
You seem to be under the impression that Java Preferences is a general tool to manipulate the Windows Registry. It is not. It just so happens that the default implementation of Preferences on the Windows platform happens to store its data in the Windows Registry.
The implementation on Windows stores stuff under the following Registry paths:
For systemRoot: HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs
For userRoot : HKEY_CURRENT_USER\Software\JavaSoft\Prefs
(note: The registry paths changes a bit if you are using a 32bit JRE on a 64-bit OS but that has nothing to do with Java and everything to do with Windows. Sun's code always use the above paths.)
The point to make is that you can maybe use Java Preferences interface to read or change values in the Windows Registry but only below the above registry paths. The reason why I say 'maybe' is that this is just how it happens to be at the moment. Sun/Oracle could at any point in time decide to not to use the Windows Registry or use the Windows Registry but without using sub-nodes, i.e. store everything in one big XML string or something. The point is that Java Preferences is designed to shield you from this.
A lot of Java software that use the Java Preferences provide their own implementation (which is pretty simple to do) to avoid Sun's default implementation that uses the Windows Registry. Not everyone can write to the Windows Registry these days so that was a pretty bad design decision on Sun's part. Fortunately very easy to change.
Is there a way to determine programatically if a particular directory is actually remotely mounted? Can this be done with Java, and if not can it be done with native C code over JNI?
Since this is Java it could be running under Linux or Windows or Mac, so a proper solution needs to address all these platforms. (Again if its multiple separate solutions with C over JNI thats ok). And there may be different cases like with NFS or samba or anything else.
Thanks.
for Linux, and possibly Macintosh, you can use system library through JNI.
The relevant system call is getmntent, described here.
There is a field in mntent you can use to check to see if mount point is from device or a server, mnt_fsname, in a similar field you can get filesystem type, `mnt_type"
For Linux, you can parse /etc/mtab to find the filesystem type (nfs, smb, etc.) and match it against known network filesystem types in your program.
EDIT: column 2 is what you want in /etc/mtab
I need that as well and might end up implementing it using this command:
df -k
That works under Linux, Mac OS and Solaris.
Maybe this is something else that will be added to JDK 7 since they're also going to support symlinks.
Looking for a way to read the unique ID / serial# of a USB thumb drive;
please note that
- I am looking for the value of the manufacturer, not the one Windows allocates for it.
- I need to support multiple OS (Windows, Unix, Mac), thus needs to be a Java solution
The idea is to be able to distinguish between different USB thumb drives.
RXTX is the way to go. In the world of model trains, JMRI (Java Model Railroad Interface) has become very popular. JMRI runs on all platforms (Windows, Linux and Mac) and communicates with a variety of USB based devices (command stations). RXTX is in fact used by JMRI.
You might give a look at the following projects:
javax-usb and jusb. They seem to support Linux and Windows.
Anyway, since USB access in Java requires the use of native libraries, you might not achieve the required portability.
I've never tried using it (it's been on my todo list for a good few months now), but there is the "marge" project on java.net:
http://marge.java.net/
This should let you connect to bluetooth devices (although I don't think it is 100% feature complete, there is demo code on there), and then the ClientDevice class has a "getBluetoothAddress" method which I believe should be unique to that device
http://marge.java.net/javadoc/v06/marge-core/net/java/dev/marge/entity/ClientDevice.html
As I say though, I've never tried it...
I have never investigated this thoroughly, but from memory the RXTX library implementation of the javax.comm packages are supposedly very good and now have USB support.