Fun Coding: Invoking cli or any process through Java Program
its quite fun working on webdriver, some times you feel like automating the process of getting your system ON
let us see how to do some OS level process using simple java code, happy coding.
package com.funcoding;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class PsExec {
public void execute() throws IOException
{
//use "the path of exe file (windows)", "CMD" , "Parameters")
ProcessBuilder pb = new ProcessBuilder("C:\\PsTools\\PsExec.exe", "ping", "www.google.com");
Process p = pb.start();
InputStream is=p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=br.readLine();
System.out.println(line);
while (line!= null)
{
System.out.println(line);
}
}
public static void main(String args[]) throws IOException
{
PsExec p=new PsExec();
p.execute();
System.out.println("completed!");
}
}
Download exe here, which is used in example above: Download PsTools