I'm trying to get a perl script to run using java Runtime exec. I have a thread for each stream (stdout, stderr, stdin) and all are operating off a single storage (screen object). The real perl script may or may not prompt for input. For testing, I used a perl script that does the following:
output to stdout
output to stderr
output to stdout
input from stdin
output to stdout
The perl script works from the command line. It hangs when I run it thru Java Runtime exec. If I comment out the "input to stdin", it works fine. I'm running on NT but I see the same thing on Unix. I then created a bash shell that does the same 5 steps, it works fine (stdin included) thru this same java program (that doesn't work for perl script). I also created a little java application that does the same 5 steps, it also works fine thru this java program. I then created a different perl script that replaces the "input from stdin" line with "input form file - with appropriate open..", and that works fine thru this java program.
Does perl need some special line termination ? Has anyone out there gotten a perl script input to work thru a Runtime exec process ?
yep - I have just got it to work no problem. My perl script is very simple, and has the same functionality as yours. The code I use to write to the stdin of the perl script is like this:
Re: Runtime exec launching perl script - problem with stdin
Nov 12, 2001 9:40 AM
(reply 2
of 4) (In reply to
#1 )
Thanks for the quick reply but I'm still having a problem with the perl script. I was using PrintWriter with autoflush before. With the change to manual flush, I now have a problem with the perl script giving an IO exception when I comment out the "input from stdin" - although all works fine with the external java code call and bash script call. Anyway, I've reduced the code to a single thread (RunScriptSimple, below). I've also included the perl script (PerlTest.pl), the standalone java code (JavaTest.java) and the bash script (BashTest.sh). Just comment/uncomment the "cmd.." line at top depending on which you want to run.
As it is, the perl script hangs. If you comment out the "$gotit = <STDIN>" in perl script; you get an IO exception. If you then comment out the "os.flush" in RunScriptSimple, the perl script runs. The other two just run fine.
System.out.println( "********* Start of Test ********" );
Runtime rt = Runtime.getRuntime();
Process proc = null;
try {
proc = rt.exec( cmd );
InputStreamReader isrStdout = new InputStreamReader( proc.getInputStream() );
InputStreamReader isrStderr = new InputStreamReader( proc.getErrorStream() );
OutputStream os = proc.getOutputStream();
BufferedReader brStdout = new BufferedReader( isrStdout );
BufferedReader brStderr = new BufferedReader( isrStderr );
String line = null;
String msg = "HELLO\n";
line = brStdout.readLine();
System.out.println( line );
line = brStderr.readLine();
System.out.println( line );
line = brStdout.readLine();
System.out.println( line );
os.write( msg.getBytes() );
os.flush();
line = brStdout.readLine();
System.out.println( line );
} catch ( IOException io ) {
System.out.println( "got IO exception " io );
io.printStackTrace();
}
System.out.println( "******** End of Test ********" );
}
}
--------- PerlTest.pl ----------
#
print "Start of perl script\n";
print STDERR "This is to STDERR\n";
print "Before input\n";
$gotit = <STDIN>;
print "Got : $gotit \n";
Re: Runtime exec launching perl script - problem with stdin
Nov 28, 2001 9:01 AM
(reply 3
of 4) (In reply to
#2 )
This problem was solved - thanks to rob_canoe2 (above) - by adding 3 lines to the top of the perl script:
use IO::Handle;
STDOUT->autoflush(1);
STDERR->autoflush(1);
explanation was that the problem "is caused by the way
perl is buffering its output. The perl output buffer never gets filled up, so your blocking call to read the stdout from the perl program waits forever."