简体   繁体   中英

Passing the java command exit code to batch ERRORLEVEL

I am running a batch to be executed with one of our automated process, inside this batch I am calling for a java command (application). The automated processing should see the final exit code and determine if its success = 0 or not which will be a failure. In this case it does determine that and batch resume to the next line like nothing happened.

Here is the batch

@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 

%ERRORLEVEL% should contain the exit code of the last command executed by your batch file - in this case, the execution of the jar file.

As seen in your output, your call to the java executable resulted in the legal return code 0 .

(Note that you do have an echo command immediately after your java call, but this echo call does not replace the %ERRORLEVEL% value).

As mentioned by both schtever and Jim Garrison , since your java app terminates successfully without any exceptions, the return code it returns is dependent on your java application exit code - which will be 0 if not explicitly stated via System.exit(1) .

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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