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
- start a new Windows command process,
- 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
- execute
ant
in this directory with the specified parameters
- 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.