Can Bican
Can Bican

Reputation: 464

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.

Upvotes: 1

Views: 397

Answers (2)

syntagma
syntagma

Reputation: 24324

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).

Upvotes: 3

oz123
oz123

Reputation: 28858

You should read one of these files:

/etc/release
/etc/lsb-release

On a more recent distribution you will also find:

/etc/os-release

Upvotes: 1

Related Questions