views:

236

answers:

6

I'm wondering if there's a way to determine which version of Linux I'm running (ie differentiate between RHEL4, RHEL5) from within the JVM. I'm just looking for a consistent way to differentiate between the operating systems and their versions.

+4  A: 

I do not believe there are any system properties you can look at that will give you that information. How about just executing a cat /proc/version and parsing the output.

Rob Di Marco
Beat me to it! :)
javamonkey79
+1  A: 

Which version of the distro you're running or which version of the Kernel? I know that Red Hat used to keep their version number in a file called /etc/redhat-release. I'm not sure for the other distro's. You can get the kernel version by doing a system call on "uname -r"

MattC
+4  A: 

The JVM gives you some basic information about the operating system via

System.getProperty()

"os.name" // OS name
"os.arch" // OS architecture
"os.version" // OS version

As far as I know it doesn't offers distribution specific information. At least for debian distributions this information is stored in a file called

/etc/issue

So it may help reading any of the files where the different distros store this information.

Daff
at last, someone that actually read the question!
DavidM
+1  A: 

/prov/version is a good start. /etc/issue might also be helpful.

anthony
+1  A: 

If the System.getProperty(x) where x is "os.name", "os.arch", "os.version" doesn't work for you then you may have to resort to running a native process as suggested (cat /proc/version, or lsb_release, etc.)

Glenn
A: 

You can also have a look at lsb_release

lsb_release -a

would give you something like:

cyril@merlin:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 8.10
Release:    8.10
Codename:   intrepid
ccheneson
Will use this. Much appreciated.
Raymond
the question said "from within the JVM" and this answer is giving a command to run on the linux shell
DavidM
I used the jdk's process api to run issue the command, read the input for about 3 months. I ended up having issues because java.lang.Process isn't designed to work with scripts, specifically the process buffers were not large enough to allow me to read the result.The symptom was that on certain VMs I was get an empty string result, and on some I was not.So I've updated my approach to start /bin/bash as a process. Then I write "/usr/bin/lsb_release -a\n" to the process output stream and read from the input stream.Works much more consistently.
Raymond