0

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
Keith Fosberg
  • 89
  • 2
  • 9
  • Odd, I have code that checks for the error-level (and works), my syntax looks like ` if ERRORLEVEL 2 (` though. – Evan Knowles Aug 03 '21 at 12:58
  • Shouldn't that be `%ERRORLEVEL%` (and not `!errorlevel!`) ? – Abra Aug 03 '21 at 13:08
  • That makes no difference. both values are 0 – Keith Fosberg Aug 03 '21 at 13:09
  • 1
    Personally I'd use `If Not ErrorLevel 1 (` instead of `if !errorlevel! equ 0 (`. That is the same methodology as I used in [my answer](https://stackoverflow.com/a/68614038) to you yesterday, only now you've reversed the order, so I have too! That would mean that you wouldn't need to include another setlocal/endlocal pair, _(although you probably could have just used one anyhow!)_ Is there any particular reason why you've once again omitted all of the best practice double-quotes I also advised you about? – Compo Aug 03 '21 at 14:05
  • 1
    Additionally, as you were also advised yesterday, unless we know exactly the expanded content of the variable, `%java_cmd%`, we cannot determine for sure what has control of the errorlevel / exitcode. – Compo Aug 03 '21 at 14:18
  • That variable is c:\java\jdk13\bin\java in my case (java_home + bin\java) – Keith Fosberg Aug 03 '21 at 14:22
  • Using "not errorlevel 1" (or -1, which is what the java exits with on error) had the same effect. If I make a dummy function that does nothing but "exit /b 1" in place of the Java execution produces the expected results. – Keith Fosberg Aug 03 '21 at 14:29
  • I'm unclear on the double quotes. – Keith Fosberg Aug 03 '21 at 14:35
  • This may be OK. I think I actually stumbled across a bug in the jar. With this exact batch code, but introducing different errors, I am getting the expected errorlevel codes. I'll follow up here once that is confirmed. Thanks Compo – Keith Fosberg Aug 03 '21 at 18:20

0 Answers0