The following is a fragment of a script (windows cmd). Everything works except for one issue. after I execute the java I want to check the errorlevel and do different things based on that.
The java code does a System.exit(1) when there is an error. We use this jar frequently on our Linux servers and we can read the status fine.
In this instance, we need to run on a Windows server. I have the config for the ftp utility rigged with a bad server address to force a failure.
I have tried to get at errorlevel in a variety of ways, and always get "0". Since I know the Jar is returning a code, it's clear that errorlevel is not getting populated by the java program, but by the java runtime itself.
I only get non-zero errorlevel values if I bung up the java -jar command such that the java fails to execute (or aborts). It seems that I am getting success or failure from ~java-jar, not from the actual System.exit(-1) termination in the jar file. I also tried doing "java -cp" and aiming directly as the class with the main method.
(values of variables are sensitive, so just trust me that they are correct.) :) This function works correctly except that it never runs the error branch.
Based on comments: I changed it to this. The behavior is unchanged though.
- %java_cmd% expands to c:\java\jdk13\bin\java
- %select% expands to "EAD-dev" (tells ftp what server to use)
:moveFiles
@for /f %%R in ('dir /b current') do (
@echo ___
@echo Moving %%R to FTP server.
%java_cmd% -jar f17ftp.jar -dput -file:%%R %select%
if not errorlevel 1 (
@echo Transfer of %%R succeeded.
move "current\%%R" xmlfiles 1> nul
) else (
@echo Transfer of %%R failed.
call :readLog
%java_cmd% -cp SQLQueryXML.jar com.saliencrgt.chums.AlertMessage "Contents of ftp log are:<br/><br>""!log!"
)
)
exit /b