开发者

Handling Java stdout and stderr in Perl

开发者 https://www.devze.com 2022-12-28 12:04 出处:网络
I am trying to run a Java program from my Perl script. I would like to avoid using System.exit(1) and System.exit(-1) commands in Java. I am however printing to STDOUT and STDERR from Java. In my Perl

I am trying to run a Java program from my Perl script. I would like to avoid using System.exit(1) and System.exit(-1) commands in Java. I am however printing to STDOUT and STDERR from Java. In my Perl script, I am reading from Java's stdout and using that line by line output. How do I print stderr and fail if I ever see stderr? This is what I have so far:

my $java_command = ...;
open(DATA, ">$java_command"); 
while (<DATA>) {
    chomp($_);
    开发者_如何学JAVA....
    ....
}


Look at the perl documentation for FAQ How-can-I-capture-STDERR-from-an-external-command. This will brief you all the details about capturing and discarding STDOUT and STDERR.


This has been discussed here: How can I capture STDERR from an external command?

So you can do something like:

open (DATA, "($java_command_with_args | sed 's/^/STDOUT:/') 2>&1 |");
while (<DATA>) {
    if (s/^STDOUT://)  {
        print "line from stdout: ", $_;
    } else {

        print "line from stderr: ", $_;
        die("Saw something on stderr");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消