8

From java, I got name of the OS Iam working. See below code :

System.out.println(System.getProperty("os.name"));

In windows xp, it prints like : Windows XP

But in ubuntu/fedora, it shows only Linux.

Can anyone help me to find which linux version Iam using (like ubuntu or fedora) using java code? Is it possible to find the linux distro from java?

Haseena
  • 484
  • 2
  • 11
  • 25
  • 2
    Try `os.version`. I run Windows & am not sure what it returns on *nix. – Andrew Thompson Feb 22 '13 at 06:50
  • 1
    http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html – Brian Roach Feb 22 '13 at 06:52
  • `os.version` only returns `3.2.0-23-generic-pae`. From this how can I identify distro? – Haseena Feb 22 '13 at 06:56
  • 1
    Be sure to add @BrianRoach (or whoever) to notify them of a new comment. BTW - what relevance is this? The user already knows which Linux distro they have (if they care enough to wonder), and the app. rarely if ever needs such information. What feature are you trying to provide through knowing that information? – Andrew Thompson Feb 22 '13 at 06:59
  • Its for reading configuration file of mariadb is in different locations. For getting this file, I need the linux distro. – Haseena Feb 22 '13 at 07:05
  • Are you selectively ignoring my comments? I advised you to add a notification. I don't need to add one, since the person who asked the question (you) is automatically notified of new comments. -- So really, it is not the name you want, but the **path to a DB.** Pays to mention that in the question. It might be relevant. – Andrew Thompson Feb 22 '13 at 07:11
  • @Haseena - quite frankly, good luck with that. Have it passed in from the command line or use a config file that specifies it. You can try using `uname` but that's going to be problematic at best – Brian Roach Feb 22 '13 at 07:11
  • @BrianRoach What do you mean by 'using `uname`'? *Ignore that - I just saw Andrew Mao's answer which clarified.* Haseena, I just saw a [page](https://kb.askmonty.org/en/starting-and-stopping-mariadb-automatically/) that suggests MariaDB could be installed in non-standard locations, so good luck determining the path from an OS name! – Andrew Thompson Feb 22 '13 at 07:16
  • I suppose you could scan the entire filesystem :) – Brian Roach Feb 22 '13 at 07:21
  • Please allow us to Google that for you: [determine linux distro filetype:java site:github.com](https://duckduckgo.com/?q=determine+linux+distro+filetype:java+site:github.com) reveals [wille | oslib](https://github.com/wille/oslib). Oslib is desribed as *"Java library to easily detect running Operating System, BSD Flavor, Linux Distribution, Desktop Environment and Architecture"*. – jww Jul 03 '19 at 19:03

4 Answers4

7

Starting from this, I extending the code to include different fallback scenarios in order to get the OS versions on several platforms.

  • Windows is descriptive enough, you get the info from the 'os.name' system property
  • Mac OS : you need to keep a list of the release names according to the version
  • Linux : this is the most complicated, here is the list of fallbacks:
    - get the info from the LSB release file if the distro is LSB compliant
    - get the info from the /etc/system-release if it exists
    - get the info from any file ending with '-release' in /etc/
    - get the info from any file ending with '_version' in /etc/ (Mostly for Debian)
    - get the info from /etc/issue if it exists
    - worst case, get what info from /proc/version is available

  • You can get the utility class here:
    https://github.com/aurbroszniowski/os-platform-finder

    Aurelien
    • 101
    • 2
    • 5
    • He is explicitly asking how to get the Linux distribution name(Like ubuntu, linux and macOS). more info [here](https://www.linux.com/what-is-linux/) – Rishon_JR Dec 30 '22 at 14:54
    6

    This code can help you:

    String[] cmd = {
    "/bin/sh", "-c", "cat /etc/*-release" };
    
    try {
        Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader bri = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
    
        String line = "";
        while ((line = bri.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException e) {
    
        e.printStackTrace();
    }
    

    UPDATE

    It if you only need the version try with uname -a

    UPDATE

    Some linux distros contain the distro version in the /proc/version file. Here is an example to print them all from java without invoking any SO commands

    //lists all the files ending with -release in the etc folder
    File dir = new File("/etc/");
    File fileList[] = new File[0];
    if(dir.exists()){
        fileList =  dir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String filename) {
                return filename.endsWith("-release");
            }
        });
    }
    //looks for the version file (not all linux distros)
    File fileVersion = new File("/proc/version");
    if(fileVersion.exists()){
        fileList = Arrays.copyOf(fileList,fileList.length+1);
        fileList[fileList.length-1] = fileVersion;
    }       
    //prints all the version-related files
    for (File f : fileList) {
        try {
            BufferedReader myReader = new BufferedReader(new FileReader(f));
            String strLine = null;
            while ((strLine = myReader.readLine()) != null) {
                System.out.println(strLine);
            }
            myReader.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
    
    PbxMan
    • 7,525
    • 1
    • 36
    • 40
    • 1
      `if (file.getName().endsWith("-release")) `...seems simpler than all the stuff with `Process` and `BufferedReader`. – Andrew Mao Feb 22 '13 at 07:26
    0

    One ad-hoc way of getting Linux distro name is to read the content of /etc/*-release file. It will give you something like CentOS release 6.3 (Final).

    Reading content of this file from Java is straight forward.

    Might not be the best way, but it will get the job done, also this works only on *nix box and not on windows.

    0

    You can use java to run uname -r, and get the results; that usually reveals the distro unless it was compiled by some dude from source in his basement. For my machine:

    mao@korhal ~ $ uname -r
    3.4.9-gentoo
    

    and to run it:

    Process p = Runtime.getRuntime().exec("uname -r");  
    BufferedReader in = new BufferedReader(  
                        new InputStreamReader(p.getInputStream()));  
    String distro = in.readLine();  
    
    // Do something with distro and close reader
    

    Edit: perhaps uname -a would work better here for distros in general. Or look at /etc/*-release files, which seems to be generally defined on most generals.

    Andrew Mao
    • 35,740
    • 23
    • 143
    • 224