3

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dev
  • 1,477
  • 5
  • 18
  • 43

1 Answers1

4

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.

ikegami
  • 367,544
  • 15
  • 269
  • 518