my600080 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
When I used java inline eval {};, I cannot get the whole exception stack trace. For example,
eval {
my $uri = new java::net::URI($fileURI);
};
if ( $@ ) {
print $@->getMessage();
# But how can I print the whole stack trace?
}
I tried to use $@->printStackTrace() but it kept generating the following error: Can't call method "getStackTrace" without a package or object reference.

Thanks for the help!

Replies are listed 'Best First'.
Re: java inline printstacktrace
by my600080 (Novice) on Mar 30, 2010 at 15:30 UTC

    I found the solution. Share with everyone.

    eval { #call java jar file }; if ($@){ print " Getting error ...\n"; if ( caught("java.lang.Throwable") ) { # have to save it; otherwise value will change after t +he following two lines my $exception = $@; my $msg = $exception->getMessage(); my $string_writer = new java::io::StringWriter(); my $print_writer = new java::io::PrintWriter($string_w +riter); $exception->printStackTrace($print_writer); my $line = $string_writer->toString(); $errorLog->error($msg . "\n" . $line); } }
      Sounds like ->getMessage(), java::io::StringWriter::new or java::io::PrintWriter::new was using eval and clobbering $@, thus the need to copy it.
        That was the theory put forward on the Inline mailing list.