title: Shell script not recording Java exit status on failure
body: <p>I am trying to write a shell script that records the exit status of a Java program. The script should simple launch a Java app, and if the Java app doesn't run for some reason, the shell script should detect this and take mitigating measures.</p>
  
  <h2>The following is my script:</h2>
  
  <pre><code>#!/bin/bash 
  
  APPNAME="app"
  APPFOLDER=$APPNAME
  BACKUP=$APPFOLDER"-backup"
  LOGFOLDER=$APPNAME"-log"
  
  echo "Starting new app"
  java -jar $APPFOLDER/$APPNAME*.jar &gt; $LOGFOLDER/$APPNAME"_$(date+%Y.%m.%d.%s).log"
  wait 
  STATUS=$?
  if [ $STATUS -eq 0 ]
     then
        echo "Deployment successful" $?
     else
        echo "Deployment failed: ... derp" $?
  fi
  </code></pre>
  
  <p>I have written a simple Swing GUI that runs fine. However, I packaged it as a jar without specifying an entry point. Hence, I should get the error:  </p>
  
  <pre><code> Exception in thread "main" java.lang.NoClassDefFoundError: Demo$1
  </code></pre>
  
  <p>and the script should detect that the application failed to start. </p>
  
  <p>All of this works FINE until I try to launch the Java app in the background using &amp;. Whenever I do this:  </p>
  
  <pre><code>java -jar $APPFOLDER/$APPNAME*.jar &gt; $LOGFOLDER/$APPNAME"_$(date+%Y.%m.%d.%s).log" &amp; 
  </code></pre>
  
  <p>the script always returns a 0 for $?, indicating it passed.<br>
  What am I doing wrong? Is there a better way to go about detecting if the app failed to launch?</p>
  
  <p>Thanks!</p>