Search code examples
linuxbashjarerror-detection

Detecting failure of JAR command in Bash


I am executing this simple jar command in bash:

jar cvfm control.jar Manifest <class list>

How can I automate the detection of a failure in the execution of this command? By failure i mean that the command just simply failed to execute due reasons such as:

  • an error in the Manifest,
  • improper usage of the jar command itself,
  • some random error like, ClassNotFoundError, etc.

Mind you, I am not interested in knowing if the right set of classes were archived or not. So I will not consider that to be a failure.

I am currently just using the verbose option to inspect if everything went well or not. But, I find this to be time consuming. I just want to know whether or not the jar command was successful in archiving some set of class files or not. Is there a way to do this in bash?


the closest other resource regarding this issue i found was this: How to detect error if archive of jar file failed?. But, i have no clue about batch files and i am interested in doing this in the bash shell.


Solution

  • This is the most basic way to see if a command suceeded:

    if jar cvfm control.jar Manifest <class list> ; then
        echo "Success"
    else
        echo "Oops!"
    fi
    

    If you only want to detect failure, then invert the test:

    if ! jar cvfm control.jar Manifest <class list> ; then
        echo "Oops!"
    fi
    

    Or test the result explicitly:

    jar cvfm control.jar Manifest <class list>
    if (( $? )) ; then
        echo "Oops!"
    fi