eval { #call java jar file }; if ($@){ print " Getting error ...\n"; if ( caught("java.lang.Throwable") ) { # have to save it; otherwise value will change after the following two lines my $exception = $@; my $msg = $exception->getMessage(); my $string_writer = new java::io::StringWriter(); my $print_writer = new java::io::PrintWriter($string_writer); $exception->printStackTrace($print_writer); my $line = $string_writer->toString(); $errorLog->error($msg . "\n" . $line); } }