How to launch a batch file in a Selenium or Java?

A batch file is a script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. Extension of batch file is .bat.

There is no such library or command in Selenium to execute batch file but you can do it using Java. Below is the code:

Process batch = Runtime.getRuntime().exec("path of the batch file");


Process: provides control of native processes started by ProcessBuilder.start and Runtime.exec. The class provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process. 

RuntimeEvery Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method.

exec(): Executes the specified string command in a separate process.

waitFor(): Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the process has already terminated. If the process has not yet terminated, the calling thread will be blocked until the process exits