SUSHANT SRIVASTAVA SUSHANT SRIVASTAVA - 5 months ago 62
Java Question

Command prompt do not close after running batch file which have 'exit' in last line

I am running a batch (ScanProject.bat) file using java by following code

Process p= Runtime.getRuntime().exec("cmd /c start /wait ScanProject.bat "+ BaseProjDir+"\\"+jo.getString("Name")+" "+st.nextToken());

System.out.println("Exit value : "+p.waitFor());


And following is the batch file code :-

%2:
cd %1
ant -f ..\antbuild.xml analyse
exit


Batch file run successfully but problem is command prompt window do not closes automatically and hence Process do not terminated automatically and my program wait for infinite time to complete this process.Please suggest me any technique so that cmd exit after running ant -f ..\antbuild.xml analyse command.

Thanks.

Answer

cd /D "Full path of directory" or pushd "Full path of directory" with popd before exit is better to switch the current directory to any directory on any drive (cd and pushd/popd) or even to a network share (just pushd/popd). Run in a command prompt window cd /? and pushd /? for details.

cmd /C starts a new Windows command process with closing the process automatically after last command was executed. Run in a command prompt window cmd /? for details on options of Windows command interpreter.

start is a command to start a new Windows command process or a GUI/hybrid application in a separate process.

So what you do here is starting a new Windows command process which starts a new Windows command process.

Running in a command prompt window start /? outputs the help for this command. start interprets often the first double quoted string as title string for the new command process. This causes often troubles on command lines with at least 1 double quoted string. Therefore usage of start requires often an explicit definition of a title string in double quotes as first argument for start which can be even an empty string, i.e. simply "" as first argument after start.

As it can be read after running exit /? in a command prompt window, this command without /B always exits the current Windows command process immediately. So when ant.exe finished, the command process in which the batch file was processed is definitely terminated.

I'm having no experience on Java development, but in my point of view it should be enough to use the following execution command which does not need a batch file at all.

The Java code line

Process p= Runtime.getRuntime().exec("cmd.exe /C cd /D \"" + jo.getString("Name") + "\" && ant.exe -f ..\\antbuild.xml analyse"); 

should be enough to

  1. start a new Windows command process,
  2. set the current directory within this command process to the drive and directory specified by jo.getString("Name") which of course must return a directory path with drive letter and using backslashes as directory separators, and on success
  3. execute ant in this directory with the specified parameters
  4. with terminating the Windows command process automatically after console application ant.exe finished if ant.exe is a console application.

I'm not sure if cmd.exe /C is needed at all.

I suggest to test this command first manually from within a command prompt window. Then use it in the Java application if really working and producing the expected result. And finally I would further test if cmd.exe /C is needed at all in Java code.

See Single line with multiple commands using Windows batch file for details about the operator && to run a command after previous command was successful. And see also Why do not all started applications save the wanted information in the text files as expected? for an explanation of console / GUI / hybrid application.

NOTE: There is also Java Runtime method exec(String[] cmdarray, String[] envp, File dir) to execute a command like ant.exe with its parameters -f and ..\antbuild.xml and analyse in the directory defined with third parameter which might be better for this task.