You could start a process in the background that waits for a certain amount of time and then kills the parent. The parent just needs to kill the process as soon as it finishes the actual job.
For example:
#!/bin/sh
# remember PID of main process
main_pid=$$
kill_after() {
sleep 3
echo "Killing after 3 seconds of idle."
kill ${main_pid}
}
# start kill process and remember killer PID
kill_after &
killer=$!
# your main process (for testing, `sleep 5`)
#java -jar myjar.jar
sleep 5
# kill killer so it doesn't attempt to kill the main
# process anymore
kill $killer
Note that $$
returns the PID of the current process and $!
returns the PID of the just launched procedure (kill_after
in this case).