Piping input to a Java app with Perl

Posted by user319479 on Stack Overflow See other posts from Stack Overflow or by user319479
Published on 2010-04-18T01:25:47Z Indexed on 2010/04/18 1:33 UTC
Read the original article Hit count: 494

Filed under:
|

I need to write a Perl script that pipes input into a Java program. This is related to this, but that didn't help me. My issue is that the Java app doesn't get the print statements until I close the handle. What I found online was that $| needs to be set to something greater than 0, in which case newline characters will flush the buffer. This still doesn't work.

This is the script:

#! /usr/bin/perl -w

use strict;
use File::Basename;

$|=1;

open(TP, "| java -jar test.jar") or die "fail";
sleep(2);

print TP "this is test 1\n";
print TP "this is test 2\n";
print "tests printed, waiting 5s\n";
sleep(5);

print "wait over.  closing handle...\n";
close TP;
print "closed.\n";

print "sleeping for 5s...\n";
sleep(5);
print "script finished!\n";
exit

And here is a sample Java app:

import java.util.Scanner;

public class test{

    public static void main( String[] args ){

        Scanner sc = new Scanner( System.in );
        int crashcount = 0;
        while( true ){
            try{
                String input = sc.nextLine();
                System.out.println( ":: INPUT: " + input );
                if( "bananas".equals(input) ){
                    break;
                }
            } catch( Exception e ){
                System.out.println( ":: EXCEPTION: " + e.toString() );
                crashcount++;
                if( crashcount == 5 ){
                    System.out.println( ":: Looks like stdin is broke" );
                    break;
                }
            }
        }
        System.out.println( ":: IT'S OVER!" );
        return;
    }

}

The Java app should respond to receiving the test prints immediately, but it doesn't until the close statement in the Perl script. What am I doing wrong?

Note: the fix can only be in the Perl script. The Java app can't be changed. Also, File::Basename is there because I'm using it in the real script.

© Stack Overflow or respective owner

Related posts about perl

Related posts about java