Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Trapping Java Errors

by aardvark (Pilgrim)
on Mar 31, 2001 at 02:00 UTC ( #68517=perlquestion: print w/replies, xml ) Need Help??
aardvark has asked for the wisdom of the Perl Monks concerning the following question:

I am executing Java commands using backticks in Perl. I eval the command and look for errors in $@. But the error/warnings always go to the screen and never into $@.

I am trying to use some of the command line tools of the project; Xerces-J, Xalan and FOP. The goal is to validate the XML, transform it, then create a PDF. I wish I could use AxKit, but I need to create PDFs and FOP is the only open source tool I could find to do this. One of the routines look like this;

sub validate_xml { my $file_r = shift; # use Xerces to validate xml eval { `java sax.SAXCount -v $$file_r`; }; if ($@) { print "$@\n"; &log_error($@); } }
It does the job, but I am going crazy trying to trap any errors or warnings. When there is an error it just prints it to the screen and it never gets into $@. I have read up on eval and system but I cannot figure out a way to get the results of the command into $@. I need to do error checking and logging for each of these routines. Is it a problem specific to Java commands, am I missing something in my eval? Or did I just drink too much coffee today? If anybody can help me I will toast to your collective honor and wisdom this weekend. mmmmmm ... toast

Get Strong Together!!

Replies are listed 'Best First'.
Re (tilly) 1: Trapping Java Errors
by tilly (Archbishop) on Mar 31, 2001 at 20:12 UTC
    Your problem is that the tool is sending errors out on STDERR while you read STDOUT. If you want to trap the error you will need to trap both filehandles.

    This sounds like the kind of thing that I have used IPC::Open3 for in the past. Now be aware that doing this is somewhat tricky. As soon as you use that module you need to figure out how to handle STDIN, STDOUT, and STDERR. Just to make life fun, if are trying to read on one of the pipes while it tries to write on another, you may be waiting for a good while. Also you need to think about the fact that when the poor child dies you need to reap it with wait or waitpid.

    This definitely deserves a generic "run_system" command. And since all errors will be coming from there, you want to use Carp and confess to any problems.

    Therefore I recommend looking for ways to not have to do that work yourself. (Push it off to the OS. After all that is why you have one!) Personally I would use File::Spec to pick up the devnull function to get null data for STDIN. I would use File::Temp to create a tempfile for STDERR (which you can seek on then read at your leisure), and I would read STDOUT yourself.

    Then your pattern looks like this:

    1. Get the system command.
    2. Open the handles it needs.
    3. open3.
    4. read STDOUT.
    5. Do a wait or waitpid to collect the zombie.
    6. Check $? and read what was printed to STDERR. Handle the error if there was one.
    7. Return what came from STDOUT.
    In case it isn't obvious, I have written this kind of thing in the past. IIRC it wasn't portable (worked on Linux, but not under Windows, never bothered to find out why) so consider yourself warned that I have never bothered to do this portably. But doing it, even non-portably, was quite instructive...
Re: Trapping Java Errors
by MeowChow (Vicar) on Mar 31, 2001 at 02:14 UTC
    Perhaps this will help.

    side note: $$file_r is untainted, right?

                   s aamecha.s a..a\u$&owag.print
      The files are untainted. I'm getting them from a directory using 'opendir' and 'readdir'. I then run each file through a regex and only grab the files that match my naming convention. There is no CGI/user input stuff going on here. Thanks for asking though 8-)
      This node helped me figure out if my files were tainted or not.

      Get Strong Together!!

(jeffa) Re: Trapping Java Errors
by jeffa (Bishop) on Mar 31, 2001 at 02:16 UTC
    If you know the class file will be in the same directory as your Perl script you can open a pipe instead of using eval and backticks:
    open(JAVA,'|java Hello') or die "oops: $!\n"; print while <JAVA>; close JAVA;
    chdir to the directory where the class file is if it is in a diferent directory than your Perl script.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://68517]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-04-19 16:14 GMT
Find Nodes?
    Voting Booth?