从java中查找linux发行版的名称
我们正在用java编写一个小型库,需要从底层系统收集信息。我们可以从 java 的系统属性中读取大部分内容,但在 Linux 上运行时,我们似乎找不到提取发行版名称的正确方法。该调用
System.getProperty("os.name");
返回“Linux”(我们也收集),但我们正在寻找一种方法来获取“Ubuntu”等。我们需要 Java 中的这个解决方案,并且不想进行一些 /etc/release 解析
We are writing a small library in java that needs to collect information from the underlying system. We are able to read most of the stuff from system properties in java, but we cannot seem to find the right way to extract the name of the distro when run on linux. The call
System.getProperty("os.name");
return "Linux" (which we also collect) but we are looking for a way to get e.g. "Ubuntu" as well. We need this solution in java and would like to not have to do some /etc/release parsing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要可靠且准确地做到这一点是不可能的,我能建议的最好方法是获取“uname -a”的输出并使用它。
注意:这不是 Java 的限制 - 根本没有通用(且准确)的方法来识别发行版。
To do this reliably and accurately is impossible, the best I can suggest is to take the output of 'uname -a' and use that.
Note: This is not a Java limitation - there is simply no common (and accurate) means of identifying a distribution.
我通常使用以下命令:
用 Java 读取这个文件应该很容易。问题是这个文件是否存在于每个(或至少大多数)Linux 发行版上。我在任何需要的地方都能找到它,但并不经常需要它。
I usually use the following command:
Reading this file in Java should be quite easy. The question is whether this file is on every (or at least majority) of Linux distributions. I found it everywhere I needed it, though, it was not needed very frequently.
您可以尝试调用
lsb_release -i
,但这不能保证有效。You can try invoking
lsb_release -i
, but this is not guaranteed to work.