Thursday, 16 June 2016

Executing Windows and Linux commands in Java

Below example illustrates how to execute the Windows and Linux commands in Java.Note that we are using org.apache.commons.lang.SystemUtils class to determine OS type.

if(SystemUtils.IS_OS_LINUX) {
            cmd= [ "/bin/bash", "-c", " ps -fu`whoami`|grep p1|grep -v grep" ]
        } else if (SystemUtils.IS_OS_WINDOWS) {
            cmd= ["bash", "-c", "tasklist | grep p1"]
        }

 StringBuffer output = new StringBuffer();
        try {
            Process p = Runtime.getRuntime().exec(cmd);
            List<String> result = IOUtils.readLines(p.getInputStream());
            for (String line : result) {
                System.out.println(line);
                output.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return output.toString();


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

Contributors