将Java命令出口代码传递给批处理ERRORLEVEL

时间:2018-06-21 01:11:39

标签: java batch-file exit-code

我正在运行要通过我们的自动化流程之一执行的批处理,在此批处理中,我需要一个Java命令(应用程序)。自动化处理应查看最终的退出代码,并确定其成功= 0或不成功(这将是失败)。在这种情况下,它确实确定并像没有任何反应一样将批处理恢复到下一行。

这是批次

@ECHO Off
SET Talend_Job=%1%
E:
CD E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts\
for /f "tokens=2" %%2 in (E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts\Results\%Talend_Job%.txt) do (
    set  LABEL_OR_ID=%%2
    goto gotrev
)

:gotrev
echo  LABEL_OR_ID is %LABEL_OR_ID%

java -jar E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts\TalendTacTasks-1.0.1.jar ^ get_task_status ^ http://dc01talndtlv01.mycompany.loc:8080/org.talend.administrator/ ^ %LABEL_OR_ID%

set exitcode=%ERRORLEVEL%
echo %exitcode%
echo %ERRORLEVEL%
exit /B %ERRORLEVEL%

----------my results log ---------------
C:\Users\Autosysdevsvc>SET Talend_Job=autosys_talend_test_with_error 

C:\Users\Autosysdevsvc>E:

E:\>CD E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts\ 

E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts>for /F "tokens=2" %2 in (E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts\Results\autosys_talend_test_with_error.txt) do (
set  LABEL_OR_ID=%2  
 goto gotrev 
) 

E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts>(
set  LABEL_OR_ID=1529519219417_Y6RLK  
 goto gotrev 
) 

E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts>echo  LABEL_OR_ID is 1529519219417_Y6RLK 
 LABEL_OR_ID is 1529519219417_Y6RLK

E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts>java -jar E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts\TalendTacTasks-1.0.1.jar  get_task_status  http://dc01talndtlv01.mycompany.loc:8080/org.talend.administrator/  1529519219417_Y6RLK 
ERROR: Execution Status: JOB_ERROR: Job ended with error(s) - Job Exit Code: 1

E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts>echo Exitcode= 
Exitcode=

E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts>set exitcode=0 

E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts>echo ERRORLEVEL=0 
ERRORLEVEL=0

E:\Operations\BatchServices\Talend\Batches\Talend_AutoSys_Integration\scripts>exit /B 0 

1 个答案:

答案 0 :(得分:1)

%ERRORLEVEL%应该包含批处理文件执行的最后一个命令的退出代码-在这种情况下,就是jar文件的执行。

从输出中可以看出,对java可执行文件的调用导致了合法的返回码0

(请注意,在Java调用之后确实有一个echo命令,但是此echo调用不会替换%ERRORLEVEL%的值)。

正如schteverJim Garrison所述,由于您的Java应用程序成功终止,没有任何异常,因此它返回的返回代码取决于您的Java应用程序退出代码-将为{{1} },如果未通过0明确声明。

参考文献: