Results 1 to 9 of 9
  1. #1
    Sixtease's Avatar
    Sixtease is offline Member
    Join Date
    Mar 2008
    Location
    Breclav, Czech Republic
    Posts
    3
    Rep Power
    0

    Default Output Redirection

    Hello all I'm new here and would like to ask for your advice.

    I'm writing a Perl application that embeds a Java program. (I'm using Inline::Java for those interested in Perl). The interaction works so that the Perl process communicates with JVM via a socket, I have access to public methods of classes of my choice and can define my own classes. I can also set up parameters for java and javac.

    So, I'm running this class' main method and it happens to write to STDOUT. But I want the output to go into STDERR. Setting up redirection in Perl doesn't help as Java runs in a separate process and keeps its own track of filehandles.

    So my question is: Can I redirect the output from STDOUT to STDERR for a Java program using either parameters to JVM or running my own Java code? I can't modify the Java class that gives the output.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Does System.setOut(System.err) helps any way ..
    dont worry newbie, we got you covered.

  3. #3
    Sixtease's Avatar
    Sixtease is offline Member
    Join Date
    Mar 2008
    Location
    Breclav, Czech Republic
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by roots View Post
    Does System.setOut(System.err) helps any way ..
    It very well does. :-) Thanks a lot.

  4. #4
    janne is offline Member
    Join Date
    Nov 2008
    Location
    Germany, Dresden
    Posts
    5
    Rep Power
    0

    Default passthrough stdout/stderr of Perl program

    Hi folks,

    i've got a similar problem, but i want to pass through stdout/stderr of a Perl program to stdout of the calling process. The following example show the output just only after the Perl program has finished. But i want to see the output in real time.

    Java Code:
    ProcessBuilder builder = new ProcessBuilder("perl", "external.pl");
    builder.redirectErrorStream();
    Process p = builder.start();
    Scanner s = new Scanner(p.getInputStream()).useDelimiter("\\Z");
    System.out.println(s.next());
    Perl code:
    Java Code:
    #!/sbin/perl -w
    #
    use strict;
    
    foreach(0..10) {
    	sleep(1);
    	print "Timer: $_\n";
    }
    How can i concatenate these streams like this: "System.out = (PrintStream)p.getInputStream();"
    Of course this won't work. It this possible at all? Well i have to overcome process boundaries.

    Thx Janne

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    You want to concat the string representation of the streams? or that actual streams? If it's the first just do
    Java Code:
    String howdy = "System.out = " + " " + (PrintStream)p.getInputStream().toString();

  6. #6
    janne is offline Member
    Join Date
    Nov 2008
    Location
    Germany, Dresden
    Posts
    5
    Rep Power
    0

    Default

    Hi xcallmejudasx,

    the idea was like C/C++ to pipe the output of an external process to the stdout. So, I'm trying to pass through all output of a Perl program to the calling process, but just in time. If the Perl prog print a line to stdout this line should appear immediately on the java output console. That sample code above print the entire output of the Perl prog after the Perl prog has finished. I will see each line by line.
    I suppose i have to overcome process boundaries, therefore it is possible at all?
    Whats your opinion on that?

    Regards Janne
    Last edited by janne; 12-17-2008 at 06:08 PM.

  7. #7
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    That's above my knowledge level sorry but I can't help you out. We have a few posters here that probably know everything there is to programming and can help you.

  8. #8
    Sixtease's Avatar
    Sixtease is offline Member
    Join Date
    Mar 2008
    Location
    Breclav, Czech Republic
    Posts
    3
    Rep Power
    0

    Default

    First, you say you want to redirect the output of the Perl process to stdout of the caller. That makes little sense to me. Do you mean you want to redirect it to the stdin of the caller?

    Second, I am completely guessing but this may be a matter of buffering. To make perl not buffer and spit everything immediately, set the $| variable to a true value. I assume there must be some ways to configure buffering for java stdio, so explore that and set the buffer length to a minimal value.

    Hope this helps.

  9. #9
    janne is offline Member
    Join Date
    Nov 2008
    Location
    Germany, Dresden
    Posts
    5
    Rep Power
    0

    Default [SOLVED] passthrough stdout/stderr of Perl program

    I hope all of you had a nice xmas. Welcome back.

    Hi Sixtease,

    you are absolutely right. Perl buffered the output, therefore I have just only to set $|=1 to avoid buffering. Here is my sample code. Thank you very much.

    Perl code (external.pl):
    Java Code:
    #!/sbin/perl -w
    use strict;
    $| = 1; 		# don't buffer
    
    foreach(0..10) {
    	sleep(1);
    	print "Timer: $_\n";
    }
    Java code:
    Java Code:
    public static void main(String[] args) throws IOException {
    	ProcessBuilder builder = new ProcessBuilder("perl", "external.pl");
    	builder.redirectErrorStream(true);
    
    	// start process and get output via BufferedReader
    	Process p = builder.start();
    	BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    	String line = "";
    	while ((line = input.readLine()) != null) {
    		System.out.println(line);
    	}
    }
    Last edited by janne; 12-29-2008 at 11:23 AM. Reason: error in code

Similar Threads

  1. How to redirect the output
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-04-2008, 02:36 PM
  2. JSP redirection script
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-07-2008, 10:33 AM
  3. output
    By Camden in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 10:34 PM
  4. redirection in jsp
    By valery in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-06-2007, 08:31 PM
  5. stream redirection
    By Ed in forum New To Java
    Replies: 1
    Last Post: 07-02-2007, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •