killall(1) on Linux) works like
kill(1), but sends the signal to all the processes with the specified name(s), not PIDs. There are other ways of matching processes too — check the man page!
In fact, specifically because this is Java we're talking about, I'd do the following to automate things properly:
pgrep java >/dev/null && (pkill java; sleep 5)
pgrep java >/dev/null && (pkill -9 java; sleep 5)
if pgrep java >/dev/null; then
echo "Whoops, java processes still running. Bailing out."
This will check if any java processes are running, send the, the
SIGTERM signal and wait 5 seconds. If there are still processes running, it sends the
SIGKILL signal and waits another 5 seconds. If there are still processes running, it stops executing the script returning an exit code of 1.
Update: changed answer to use
pkill(1) instead of