groovy
  1. groovy
  2. GROOVY-1034

running groovy.bat does not return exit status on windows

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.0-JSR-3
    • Fix Version/s: 1.7.2, 1.6.9, 1.8-beta-1
    • Component/s: None
    • Labels:
      None
    • Environment:
      win xp sp2, java 1.4.2_09
    • Testcase included:
      yes
    • Number of attachments :
      1

      Description

      Running groovy from within another script does not give the real exit value of the groovy process. Try any of the following:

      perl -e "system('groovy -e \"System.exit(1)\"');print $?>>8"
      ruby -e "system('groovy.bat -e \"System.exit(1)\"');print $?>>8"
      groovy -e "p='groovy.bat -e \"System.exit(1)\"'.execute();p.waitFor();println p.exitValue()"

      To see that the above examples are correct in catching the exit code, try e.g.

      ruby -e "system('ruby -e \"exit(2)\"');print $?>>8"
      groovy -e "def p='ruby -e \"exit(2)\"'.execute();;p.waitFor();println p.exitValue()"

      Adding

      exit %ERROR_LEVEL%

      to the end of groovy.bat solves the problem but is not usable as it causes command prompt to exit if running groovy directly (i.e. not via some other script).

        Activity

        Hide
        blackdrag blackdrag added a comment -

        can you please test "exit /b %ERROR_LEVEL%" ?

        Show
        blackdrag blackdrag added a comment - can you please test "exit /b %ERROR_LEVEL%" ?
        Hide
        Antti Karanta added a comment -

        I already did, I just forgot to include a comment about that in the bug report. Sorry.
        It does not work, either.

        I also tried both spellings ERROR_LEVEL and ERRORLEVEL. The first should be correct (as it works when using exit %ERROR_LEVEL%), but the latter form appears in the documentation you get by typing
        exit /?

        Show
        Antti Karanta added a comment - I already did, I just forgot to include a comment about that in the bug report. Sorry. It does not work, either. I also tried both spellings ERROR_LEVEL and ERRORLEVEL. The first should be correct (as it works when using exit %ERROR_LEVEL%), but the latter form appears in the documentation you get by typing exit /?
        Hide
        Antti Karanta added a comment -

        I inspected other Java apps that start via .bat files and noticed that at least ajc (the aspectj compiler, www.eclipse.org/aspectj) does manage to return the correct exit code:

        C:\Temp>groovy -e "def p='ajc.bat'.execute();def t=p.err.text;p.waitFor();println p.exitValue()"
        -1

        // had to read the messages in the err stream, otherwise p.waitFor() does not return

        I looked at ajc.bat and noticed that running the Java class is on the last line. I experimented a little and noticed this also works w/ groovy.bat. This is quite fragile - the lines after running the Java class may contain only whitespace - not even comments.

        To emphasize: the return code is correct if the last line of the groovy.bat is
        "%JAVA_EXE%" %JAVA_OPTS% -classpath "%CP%" %CLASSWORLDS_MAIN_CLASS% groovy %CMD_LINE_ARGS%

        I hope this is of some help.

        Show
        Antti Karanta added a comment - I inspected other Java apps that start via .bat files and noticed that at least ajc (the aspectj compiler, www.eclipse.org/aspectj) does manage to return the correct exit code: C:\Temp>groovy -e "def p='ajc.bat'.execute();def t=p.err.text;p.waitFor();println p.exitValue()" -1 // had to read the messages in the err stream, otherwise p.waitFor() does not return I looked at ajc.bat and noticed that running the Java class is on the last line. I experimented a little and noticed this also works w/ groovy.bat. This is quite fragile - the lines after running the Java class may contain only whitespace - not even comments. To emphasize: the return code is correct if the last line of the groovy.bat is "%JAVA_EXE%" %JAVA_OPTS% -classpath "%CP%" %CLASSWORLDS_MAIN_CLASS% groovy %CMD_LINE_ARGS% I hope this is of some help.
        Hide
        Guillaume Laforge added a comment -

        Is it still the case that the return value is not correct?

        Show
        Guillaume Laforge added a comment - Is it still the case that the return value is not correct?
        Hide
        Antti Karanta added a comment -

        Yes, the return value is still always 0. I think the importance of this is now quite small as the native launcher behaves correctly and is included in the windows installer (which I would believe most windows users will use).

        A weird thing though is that

        groovy -e "p='groovy.bat -e \"System.exit(1)\"'.execute();p.waitFor();println p.exitValue()"

        does not work any more. It prints "255", but it seems that the problem is that groovy.bat (that is invoked in the one-liner) is not executed at all for some reason.

        The ruby version of the test works fine:

        C:\Documents and Settings\nak>ruby -e "system('groovy.bat -e \"System.exit(1)\"');print $?>>8"
        0
        C:\Documents and Settings\nak>ruby -e "system('groovy.exe -e \"System.exit(1)\"');print $?>>8"
        1

        Show
        Antti Karanta added a comment - Yes, the return value is still always 0. I think the importance of this is now quite small as the native launcher behaves correctly and is included in the windows installer (which I would believe most windows users will use). A weird thing though is that groovy -e "p='groovy.bat -e \"System.exit(1)\"'.execute();p.waitFor();println p.exitValue()" does not work any more. It prints "255", but it seems that the problem is that groovy.bat (that is invoked in the one-liner) is not executed at all for some reason. The ruby version of the test works fine: C:\Documents and Settings\nak>ruby -e "system('groovy.bat -e \"System.exit(1)\"');print $?>>8" 0 C:\Documents and Settings\nak>ruby -e "system('groovy.exe -e \"System.exit(1)\"');print $?>>8" 1
        Hide
        Tom Huybrechts added a comment -

        This is still a problem, but it can be fixed by using this as the last line in both groovy.bat and startGroovy.bat

        CMD /C EXIT /B %ERRORLEVEL%

        (copied from mvn.bat)

        any chance this can still be fixed in 1.6-RC ?

        Show
        Tom Huybrechts added a comment - This is still a problem, but it can be fixed by using this as the last line in both groovy.bat and startGroovy.bat CMD /C EXIT /B %ERRORLEVEL% (copied from mvn.bat) any chance this can still be fixed in 1.6-RC ?
        Hide
        blackdrag blackdrag added a comment -

        as last line you mean inclusive the CMD /C? that could prevent one or two problems if the bat file is called with command.com instead of cmd.exe

        Show
        blackdrag blackdrag added a comment - as last line you mean inclusive the CMD /C? that could prevent one or two problems if the bat file is called with command.com instead of cmd.exe
        Hide
        Tom Huybrechts added a comment -

        Yes, the full line, including CMD /C.

        In 1.5.7, the same problem existed, but you could fix it by adding this line to only groovy.bat.

        In Maven, the 'CMD /C' was added in MNG-3651. Not sure what problem it fixes.

        And for command.com, I hadn't heard about that since my Win98 days

        Show
        Tom Huybrechts added a comment - Yes, the full line, including CMD /C. In 1.5.7, the same problem existed, but you could fix it by adding this line to only groovy.bat. In Maven, the 'CMD /C' was added in MNG-3651 . Not sure what problem it fixes. And for command.com, I hadn't heard about that since my Win98 days
        Hide
        Philip Jenvey added a comment -

        For portability you can do %COMSPEC% /c instead of CMD /c (this is what we've done for jython.bat)

        Show
        Philip Jenvey added a comment - For portability you can do %COMSPEC% /c instead of CMD /c (this is what we've done for jython.bat)
        Hide
        Artem Petakov added a comment -

        Sorry, very new to this project. Any reason not to submit a patch for this? I am running 1.8.0 and still have the problem – I added this to my gant.bat and startGroovy.bat scripts as suggested and this fixed it.

        @rem Fix the problem that Gant does not return correct error code as documented in
        @rem http://jira.codehaus.org/browse/GROOVY-1034
        cmd /C exit /B %ERRORLEVEL%

        Show
        Artem Petakov added a comment - Sorry, very new to this project. Any reason not to submit a patch for this? I am running 1.8.0 and still have the problem – I added this to my gant.bat and startGroovy.bat scripts as suggested and this fixed it. @rem Fix the problem that Gant does not return correct error code as documented in @rem http://jira.codehaus.org/browse/GROOVY-1034 cmd /C exit /B %ERRORLEVEL%
        Hide
        Paul King added a comment -

        I added the following to all .bat files and it seemed to work for me in various tests:
        %COMSPEC% /C exit /B %ERRORLEVEL%

        Show
        Paul King added a comment - I added the following to all .bat files and it seemed to work for me in various tests: %COMSPEC% /C exit /B %ERRORLEVEL%

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            Antti Karanta
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: