6

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

wattostudios
  • 8,666
  • 13
  • 43
  • 57
soren.enemaerke
  • 4,770
  • 5
  • 53
  • 80
  • Do you know of any API (non-Java I mean) that can provide that information? – Ringding Dec 02 '10 at 11:31
  • 1
    Well, not really. From my googling it seems like doing some cat /etc/*release will get you some info but not really reliably. I was really hoping that some of you guys had bumped into this before and solved it... – soren.enemaerke Dec 02 '10 at 11:38
  • 1
    This is distribution-specific; I don't have /etc/*release at all (on Debian, even with lsb* installed). I looked to lsb_release src (it's just a python script) and it parses most of distribution information from apt. Easier way (debian-specific) than that would be looking to /etc/debian_version instead (uname -a is unreliable either) – barti_ddu Dec 02 '10 at 11:55
  • *"We are writing a small library in java that needs to collect information from the underlying system."* My initial reaction to requirements like this is to ask "Why is this kind of information any of your business?". – Stephen C Dec 02 '10 at 12:15
  • @Stephen: As condescending as that sounds, you do raise a good point: Ask about the original problem, not the perceived solution to that problem! – Arafangion Dec 02 '10 at 12:19
  • @Stephen: We are tasked with collecting usage statistics and the operating system is one of the parameters we are to collect (there is user consent for the collection) – soren.enemaerke Dec 02 '10 at 13:05

3 Answers3

9

You can try invoking lsb_release -i, but this is not guaranteed to work.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
8

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.

Arafangion
  • 11,517
  • 1
  • 40
  • 72
  • You can snoop around in /etc and deduce things based on e.g. you know which files Fedora installs, and Ubuntu installs, but you then need rules in your programs and they may break. Better to test for actual functionality. – Thorbjørn Ravn Andersen Oct 03 '11 at 12:21
4

I usually use the following command:

cat /etc/issue

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.

Jiri Patera
  • 3,140
  • 1
  • 20
  • 14