History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GROOVY-1034
Type: Bug Bug
Status: Open Open
Priority: Major Major
Assignee: Unassigned
Reporter: Antti Karanta
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
groovy

running groovy.bat does not return exit status on windows

Created: 01/Sep/05 06:49 AM   Updated: 27/Dec/07 03:21 AM
Component/s: None
Affects Version/s: 1.0-JSR-3
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments: 1. File groovy_process_exitvalue_bug.groovy (0.6 kb)

Environment: win xp sp2, java 1.4.2_09

Testcase included: yes


 Description  « Hide
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).



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Jochen Theodorou - 01/Sep/05 11:26 AM
can you please test "exit /b %ERROR_LEVEL%" ?

Antti Karanta - 02/Sep/05 12:47 AM
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 /?


Antti Karanta - 02/Sep/05 04:16 AM
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.


Guillaume Laforge - 23/Dec/07 06:17 AM
Is it still the case that the return value is not correct?

Antti Karanta - 27/Dec/07 03:21 AM
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