elder elder February 2016

JVM on OS X always exiting with status 0 (as captured in Bash script)?

I have a very simple Java class, that does nothing else but:

public class TestMain {
  public static void main(String[] args) {
    System.out.println("Running!");
    System.exit(1111);
  }
}

, packed into a TestOSX.jar file.

While on Windows I can run the above snippet and show that %ERRORLEVEL% has the expected value, I get a different outcome on OS X.

Given test.sh containing:

#!/bin/bash

"/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java" -jar TestOSX.jar 
wait $!
updater_exit_val=$?
echo $updater_evit_val

, I always print 0.

Setup: OS X 10.11.1, Oracle Java 8 u60.

What trivial detail am I missing here?

Answers


visit1985 February 2016

You do not send your java process to the background with &. Thus wait is executed after the java process exits. It can't find the process you try to wait for, because it already exited and giving return code 0 because of that. $? returns the return code of the last command (in your case wait).

You can either remove wait from your script, or you send your java process to the background by adding & at the end.

Post Status

Asked in February 2016
Viewed 1,983 times
Voted 6
Answered 1 times

Search




Leave an answer