Haseena
Haseena

Reputation: 494

Getting Linux Distro from java

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?

Upvotes: 9

Views: 7856

Answers (4)

Aurelien
Aurelien

Reputation: 101

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

    Upvotes: 7

    PbxMan
    PbxMan

    Reputation: 7623

    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());
        }
    }
    

    Upvotes: 6

    Alankar Srivastava
    Alankar Srivastava

    Reputation: 864

    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.

    Upvotes: 0

    Andrew Mao
    Andrew Mao

    Reputation: 36900

    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.

    Upvotes: 0

    Related Questions