简体   繁体   English

运行在最后一行中有“退出”的批处理文件后,命令提示符不会关闭

[英]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 我通过以下代码使用java运行批处理( ScanProject.bat )文件

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. 批处理文件运行成功,但问题是命令提示符窗口不自动关闭,因此进程不会自动终止,我的程序等待无限时间完成此过程。请建议我任何技术,以便cmd 退出运行ant -f .. \\ antbuild.xml分析命令。

Thanks. 谢谢。

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 ). cd /D "Full path of directory"pushd "Full path of directory" ,在退出之前使用popd将当前目录切换到任何驱动器上的任何目录( cdpushd / popd ),甚至是网络共享(只需按下 / popd )。 Run in a command prompt window cd /? 在命令提示符窗口中运行cd /? and pushd /? pushd /? for details. 详情。

cmd /C starts a new Windows command process with closing the process automatically after last command was executed. cmd /C启动新的Windows命令进程,并在执行完最后一个命令后自动关闭进程。 Run in a command prompt window cmd /? 在命令提示符窗口中运行cmd /? for details on options of Windows command interpreter. 有关Windows命令解释程序选项的详细信息。

start is a command to start a new Windows command process or a GUI/hybrid application in a separate process. start是一个命令,用于在单独的进程中启动新的Windows命令进程或GUI /混合应用程序。

So what you do here is starting a new Windows command process which starts a new Windows command process. 所以你在这里做的是启动一个新的Windows命令进程,它启动一个新的Windows命令进程。

Running in a command prompt window start /? 在命令提示符窗口中运行start /? outputs the help for this command. 输出此命令的帮助。 start interprets often the first double quoted string as title string for the new command process. start通常将第一个双引号字符串解释为新命令进程的标题字符串。 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, ie simply "" as first argument after start . 因此, start的使用通常需要在双引号中明确定义标题字符串作为start的第一个参数,它甚至可以是空字符串,即简单地""作为开始后的第一个参数。

As it can be read after running exit /? 因为它可以在运行exit /?后读取exit /? in a command prompt window, this command without /B always exits the current Windows command process immediately. 在命令提示符窗口中,此命令不带/B始终立即退出当前Windows命令进程。 So when ant.exe finished, the command process in which the batch file was processed is definitely terminated. 因此,当ant.exe完成时,处理批处理文件的命令进程肯定会终止。

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. 我没有Java开发的经验,但在我看来,使用以下执行命令就足够了,根本不需要批处理文件。

The Java code line Java代码行

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, 启动一个新的Windows命令进程,
  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 将此命令进程中的当前目录设置为jo.getString("Name")指定的驱动器和目录,当然必须返回带驱动器号的目录路径并使用反斜杠作为目录分隔符,并且成功
  3. execute ant in this directory with the specified parameters 使用指定的参数在此目录中执行ant
  4. with terminating the Windows command process automatically after console application ant.exe finished if ant.exe is a console application. 如果ant.exe是控制台应用程序,则在控制台应用程序ant.exe完成后自动终止Windows命令进程。

I'm not sure if cmd.exe /C is needed at all. 我不确定是否需要cmd.exe /C

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. 然后在Java应用程序中使用它,如果真的有效并产生预期的结果。 And finally I would further test if cmd.exe /C is needed at all in Java code. 最后,我将进一步测试Java代码中是否需要cmd.exe /C

See Single line with multiple commands using Windows batch file for details about the operator && to run a command after previous command was successful. 有关运算符和&&详细信息,请参阅使用Windows批处理文件的多行命令的单行,以便在上一个命令成功后运行命令。 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 . 有关控制台 / GUI / 混合应用程序的说明

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. 注意:还有Java Runtime方法exec(String [] cmdarray,String [] envp,File dir)执行命令如ant.exe及其参数-f..\\antbuild.xml并在定义的目录中analyse第三个参数可能更适合此任务。

Swap out exit for taskkill, assuming you do not have any other cmd processes running. 假设您没有运行任何其他cmd进程,请换出taskkill的exit。 Not very graceful but it will get the job done. 不是很优雅,但它会完成工作。

%2:
cd %1
ant -f ..\antbuild.xml analyse
taskkill /im cmd.exe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM