Skip to content
Advertisement

Detecting the linux distribution from java

Is there a (preferably efficient, i.e. without executing binaries and parsing their outputs) way of detecting the Linux distribution in Java? As far as I know, System provides os.name, os.arch and os.version, which don’t seem to help. For a typical Ubuntu installation they get these values:

os.name: amd64

os.arch: Linux

os.version: 3.19.0-28-generic

What I need is whether the it’s debian based, redhat family or others.

Advertisement

Answer

I think parsing a file is the way to go under Linux. You could of course do it only once.

The correct file to find such information on Linux would be /etc/*-release, which on my machine gives the following output:

$ cat /etc/*-release                                                                                        
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS"
NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.3 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

You could then parse ID_LIKE variable which would give you the exact information you need (I can’t guarantee that but I suppose it will always be in the 8th line on every Linux distribution).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement