Search code examples
javaseleniumbatch-filecmdselenium-grid2

Unable to run a .bat file in java program


Problem Statement: I just want to start the HUB and Node to perform some tests using Selenium Grid. I have two Batch files START HUB.bat and START NODE.bat which run perfectly when i manually run them. But i want them to run using a Java Program @BeforeMethod. I looked for answers

    Runtime.getRuntime().exec("cmd /C start \"./BatchFiles/START HUB.bat\"");

This Opens Up the CMD but goes to the path of my .git project but doesnt run the bat file.

I tried using Process Builder but that doesn't open the cmd.

    ProcessBuilder pb = new ProcessBuilder("cmd", "/C"," start", "START HUB.bat");
    File dir = new File("D:\\work\\GIT REPOSITORY\\project.selenium.maven.jenkinsCI\\BatchFiles");
    pb.directory(dir);
    Process p = pb.start();

Can someone please help me with this issue. Below are the commands in the batch file.

    D:
    cd work
    java -jar selenium-server-standalone-3.4.0.jar -role hub

Solution

  • So you want to execute the command line:

    cmd /C start "./BatchFiles/START HUB.bat"
    

    With cmd at beginning of the command line a new command process is already started with executing %SystemRoot%\System32\cmd.exe. This command process should automatically close after running the command as explicitly requested with option /C which means close as it can be read on running in a command prompt window cmd /?.

    The command to execute in this command process is:

    start "./BatchFiles/START HUB.bat"
    

    The internal command start of cmd.exe is for starting an executable or script in a new process. Its help can be read on running in a command prompt window start /?.

    The first double quoted string is interpreted by start as title of the new command process window opened when a batch file or a console application should be executed in a new command process.

    And this is the reason why the batch file is not executed because "./BatchFiles/START HUB.bat" is interpreted as window title string.

    And on Windows the directory separator is \ and not / as on Unix. / is used as begin of an option as you can see on /C. But Windows handles also file paths with / often correct because of replacing each / internally by \ in directory/file names with an absolute or relative path on accessing the directory or file.

    So the solution is using either

    Runtime.getRuntime().exec("cmd.exe /C start \"start hub\" \".\\BatchFiles\\START HUB.bat\"");
    

    or using

    Runtime.getRuntime().exec("cmd.exe /C \"BatchFiles\\START HUB.bat\"");
    

    A path starting with a directory or file name is relative to current directory of the running process on Windows like using .\ at begin of a directory or file name string.

    The first code starts a command process which executes the command start which starts one more command process with title start hub executing the batch file. The first command process started with cmd.exe immediately terminates after running start while the batch file is executed in second started command process. This means your Java application continues while the batch file is executed parallel.

    The second code results in executing the batch file in the single command process started with cmd.exe and halting the execution of the Java application until entire batch file execution finished.

    The usage of a batch file can be removed here by using:

    Runtime.getRuntime().exec("cmd.exe /C start \"start hub\" /D D:\\work java.exe -jar selenium-server-standalone-3.4.0.jar -role hub");
    

    With /D D:\work the working directory is defined for the command process started for executing java.exe with its parameters.

    Alternatively without using start command:

    Runtime.getRuntime().exec("cmd.exe /C cd /D D:\\work && java.exe -jar selenium-server-standalone-3.4.0.jar -role hub");
    

    Run in a command prompt window cd /? for help on cd /D D:\work and see Single line with multiple commands using Windows batch file for an explanation of operator && used here to specify two commands to execute on one line whereby java.exe is executed only if cd could successfully change the working directory to D:\work.