简体   繁体   English

Java中的子批处理文件退出代码

[英]sub batch file exit code in Java

I have a batch file A which is called in a java program with Process process = new ProcessBuilder("...").start() . 我有一个批处理文件A,该文件在Java程序中通过Process process = new ProcessBuilder("...").start()调用。

And the batch file A calls another batch file B. In my case, B's exit code is different from A's exit code since B could fail from time to time. 批处理文件A调用另一个批处理文件B。在我的情况下,B的退出代码与A的退出代码不同,因为B有时可能会失败。 My problem here is that in the java program, I cannot seem to possibly get the exit code of B by any means. 我的问题是在Java程序中,我似乎无法以任何方式获得B的退出代码。 I wonder if there's a way to achieve what I want. 我想知道是否有办法实现我想要的。

Batch A.bat calls B.bat 批处理A.bat调用B.bat
Batch B.bat will return a random errorlevel 0/1 批处理B.bat将返回随机错误级别0/1
Batch A will handover the same erorlevel to the caller/console 批次A会将相同的错误级别移交给呼叫者/控制台

:: A.bat
@Echo off
Echo about to call B.bat
Call B.bat
Echo Back in A.bat Errorlevel=%Errorlevel%=
Exit /B %Errorlevel%

:: B.bat
@Echo off
Echo this is B.bat
Set /A "RandErr=%Random% %% 2"
Echo Exit /B %RandErr%
     Exit /B %RandErr%

Sample run: 样品运行:

> a
about to call B.bat
this is B.bat
Exit /B 0
Back in A.bat Errorlevel=0=

> a
about to call B.bat
this is B.bat
Exit /B 1
Back in A.bat Errorlevel=1=

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

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