Thursday, February 26, 2015

Command execution by Java Processor

sample code to execute any command on operating system by using java processor.

for example :
To find jdk installed in system, We can run the command "where javac" for window and "where is javac" for linux
the above command will return path javac.exe/javac.bin, by using that path we have to go to root folder to find system is having jdk.

code :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 *
 * @author Uttesh Kumar T.H.
 */
public class CommandExecutor {

    public static void main(String[] args) {
        String JAVA_WINDOWS = "where javac";
        String JAVA_LINUX = "whereis javac";
        if (System.getProperty("os.name").contains("win") || System.getProperty("os.name").contains("Win")) {
            System.out.println("result : " + executeCommand(JAVA_WINDOWS));
        }else{
            System.out.println("result : " + executeCommand(JAVA_LINUX));
        }
    }

    private static String executeCommand(String command) {
            String result = getCommandOutput(command);
            if (result == null || result.isEmpty()) {
                System.out.println("NOT FOUND");
                return result;
            } else {
                return result;
            }
    }

    private static String getCommandOutput(String command) {
        String output = null;
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(command);
            output = ConvertStreamToString(process.getInputStream());
        } catch (IOException e) {
            System.err.println("Cannot retrieve output of command");
            System.err.println(e);
            output = null;
        }
        return output;
    }

    private static String ConvertStreamToString(InputStream stream) {
        BufferedReader reader = null;
        InputStreamReader streamReader = null;
        try {
            streamReader = new InputStreamReader(stream);
            reader = new BufferedReader(streamReader);

            String currentLine = null;  //store current line of output from the cmd
            StringBuilder commandOutput = new StringBuilder();  //build up the output from cmd
            while ((currentLine = reader.readLine()) != null) {
                commandOutput.append(currentLine);
            }
            return commandOutput.toString();
        } catch (IOException e) {
            System.err.println(e);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    System.err.println(e);
                }
            }
            if (streamReader != null) {
                try {
                    streamReader.close();
                } catch (IOException e) {
                    System.err.println(e);
                }
            }
            if (reader != null) {
                try {
                    streamReader.close();
                } catch (IOException e) {
                    System.err.println(e);
                }
            }
        }
        return null;
    }
}

Reactions: