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

Related Posts:

  • Find Number of duplicates present in a ArrayList To find the number of occurrences of duplicate integer present in List, first we will create temp List which will have no duplicates and then use collection frequecy() function to get the occurrence of duplicate number in or… Read More
  • Screen capture by Java robot classCame across a rare util class of java "Robot" class, which provie the control/auto typing of letter/keyboard controll and screen capering. sample code … Read More
  • Timezone Conversion with Daylight savingTimezone conversion from one timezone to other timezone for the given time or hour string input. While converting the timezone we have to consider DST standard also. in jdk 1.3. and 1.4 DST was not taken care internally by j… Read More
  • Whois domain detail by javaApache commmon org.apache.commons.net.whois.WhoisClient class provides the domain details. Following details: Domain Name: UTTESH.COM Registrar: GODADDY.COM, LLC Sponsoring Registrar IANA ID: 146 Whois Server: whois.godaddy… Read More
  • Is in Daylight Saving Time (DST)? for given date and timezone Daylight Saving Time (DST) is the practice of turning the clock ahead as warmer weather approaches and back as it becomes colder again so that people will have one more hour of daylight in the afternoon and evening during th… Read More

0 comments:

Post a Comment