Fix for JRuby compatibilty

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