dev
dev

Reputation: 1507

Return code using exec is not coming back

I have a Perl script that is calling a JAR file...

exec("$java_path/java -jar testjar.jar");

In the code I have a situation where the JAR file exits on a error (as intended). When I run the command on the Windows or Unix command line, the return code is "1". However, when I run the Perl script that calls the JAR, on Unix I get "1" but on Windows I get "0" (no error).

Note: On Windows I used "echo %errorlevel%" to get the return code immediately after running the JAR/script. On Unix I used "echo $?".

Why does this work on Unix but not on Windows?

Upvotes: 3

Views: 254

Answers (1)

ikegami
ikegami

Reputation: 385917

I can reproduce:

>perl -e"exec 'perl -eexit(1)' or die"

>echo %ERRORLEVEL%
0

I would call that a bug in Perl. Keep in mind that exec is a unix concept which has no parallel in Windows. The emulation apparently doesn't propagate the exit code. Workaround:

 use POSIX qw( _exit );

 if ($^O eq 'MSWin32') {
     system($cmd);
     _exit($? >> 8);
 } else {
     exec($cmd);
 }

Which is basically what exec does anyway.

Upvotes: 4

Related Questions