Article updated on

How to get the linux version/distro information from Java

There are many ways of getting the linux distribution in Java. Here you can find some examples that may suit your needs.

 

Getting the version from the System properties

package com.exec;
import java.util.Properties;
public class ExecRealease extends Thread {
    public static void main(String[] args) {                
        Properties props = System.getProperties();
        System.out.println(props.getProperty("os.name"));
        System.out.println(props.getProperty("os.version"));
    }
}

 

Using Runtime.getRuntime().exec(...) to get it with a system command.

This example prints all the release related files using cat under the folder etc

package com.exec;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExecRealease extends Thread {
    public static void main(String[] args) {
        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);
            }
            bri.close();
        } catch (IOException e) {
            e.printStackTrace();
        }         
    }
}

*change cat /etc/*-release for uname -a if you just need the distribution

 

Using Just Java to read the version files

Uses Java to read all the version related files including the version file included under /proc

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.util.Arrays;
public class ExecRealease extends Thread {
    public static void main(String[] args) {
        //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());
            }
        }    
    }
}