`hash` in script doesn't run properly with Process

I have a script with the following line:

hash git >/dev/null 2>&1 || { echo >&2 “Git is not installed”; exit 2; }

asserting git is installed and, if not, exiting. I want to run it with Java:

new ProcessBuilder(“script.sh”, “0”).start().waitFor()

The process always exits because git isn’t being found, even though it runs perfectly fine in bash (because it is installed).

EDIT: same with command -v and Runtime.getRuntime().exec(..) (all combinations)

It seems like your $PATH variable is not sourced in the new context created by the ProcessBuilder method.

As your script executes fine and doesn’t throw an error, even if it shows an unexpected result, it seems that hash is recognized, but that your call doesn’t know where to look for the git executable

Try rewritting your script using the full path to the function for java to be able to find the executables, i.e. /usr/bin/git