fbpx
Pages Navigation Menu

Coding is much easier than you think

Executing batch file(.bat) from Java

Executing batch file(.bat) from Java

 


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class JavaRunCommand {

public static void main(String args[]) 
{
String stream = null;
try 
{
Process p = Runtime.getRuntime().exec(
	"C:\WINDOWS\system32\cmd.exe /c start C:\Batch\test.bat");

BufferedReader stdInput=new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError=new BufferedReader(new InputStreamReader(p.getErrorStream()));

// read the output from the command Input
System.out.println("Standard output of the command:");
while ((stream = stdInput.readLine()) != null) 
{
  System.out.println(stream);
}

// read any errors from the attempted command
System.out.println("Standard error of the command (if any):");
while ((stream = stdError.readLine()) != null) 
{
  System.out.println(stream);
}
System.out.println("ended!!");
}
catch (IOException e) 
{
 System.out.println("Exception:");
 System.err.println(e.getMessage());
}
}
}

 

dwd2
Download It – JavaRunCommand

 

About Mohaideen Jamil


I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Advertisements
%d bloggers like this: