简体   繁体   English

从java检测linux发行版

[英]Detecting the linux distribution from java

Is there a (preferably efficient, ie without executing binaries and parsing their outputs) way of detecting the Linux distribution in Java? 是否存在(最好是有效的,即没有执行二进制文件并解析其输出)检测Java中的Linux发行版的方法? As far as I know, System provides os.name , os.arch and os.version , which don't seem to help. 据我所知, System提供的os.nameos.archos.version似乎没有帮助。 For a typical Ubuntu installation they get these values: 对于典型的Ubuntu安装,他们获得以下值:

os.name : amd64 os.name

os.arch : Linux os.arch :Linux

os.version : 3.19.0-28-generic os.version :3.19.0-28-通用

What I need is whether the it's debian based, redhat family or others. 我需要的是它是基于debian的,redhat系列还是其他人。

I think parsing a file is the way to go under Linux. 我认为解析文件 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: 在Linux上查找此类信息的正确文件是/etc/*-release ,在我的机器上提供以下输出:

$ 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). 然后你可以解析ID_LIKE变量,它会给你所需的确切信息(我不能保证,但我想它总是在每个Linux发行版的第8行)。

You should read one of these files: 您应该阅读以下文件之一:

/etc/release
/etc/lsb-release

On a more recent distribution you will also find: 在最近的发行版中,您还会发现:

/etc/os-release

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM