JRuby and Open3.popen3 compatibility fix

JRuby doesn't set $? until after the Open3.popen3 block has finished.
Moving the error checking logic outside fixes this problem.