participate


Java Programming [Archive] - Runtime exec launching perl script - problem with stdin
<<   Back to Forum  |   Give us Feedback
10 Duke Stars rewarded for this thread
This topic has 4 replies on 1 page.
kakstech
Posts:3
Registered: 11/10/01
Runtime exec launching perl script - problem with stdin   
Nov 9, 2001 5:02 PM

 
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 ?
 
rob_canoe2
Posts:277
Registered: 2/22/01
Re: Runtime exec launching perl script - problem with stdin      
Nov 10, 2001 10:01 AM (reply 1 of 4)  (In reply to original post )

 
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:

    String msg = "hello\n";
    OutputStream fo = proc.getOutputStream();
    fo.write(msg.getBytes());
    fo.flush();

 
kakstech
Posts:3
Registered: 11/10/01
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.

Any ideas???????


--------- RunScriptSimple ----------

import java.util.
;
import java.io.;

public class RunScriptSimple {

public static void main( String[] args ) {

String cmd = "./BashTest.sh";
// String cmd = "perl PerlTest.pl";
// String[] cmd = { "perl", "PerlTest.pl" };
// String cmd = "java JavaTest";

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";

--------- BashTest.sh ----------
#!/bin/sh
echo 'Start of Bash shell script'
echo 'Send something to STDERR' >&2
echo 'Before input'
read GOTIT
echo "Got : $GOTIT"

--------- JavaTest.java ----------
import java.io.
;

public class JavaTest {

public static void main( String [] args ) {
try {
System.out.println( "Start of JavaTest" );
System.err.println( "This is to STDERR" );
System.out.println( "Before input" );
BufferedReader r = new BufferedReader( new InputStreamReader( System.in) );
String s = r.readLine();
System.out.println( "Got line : "
s );
} catch ( Exception e ) {
System.out.println( e.getMessage() );
}
}
}

 
kakstech
Posts:3
Registered: 11/10/01
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."

Thanks Rob!!
 
ScottEllsworth
Posts:1
Registered: 7/17/97
Re: Runtime exec launching perl script - problem with stdin   
Jan 8, 2002 3:39 PM (reply 4 of 4)  (In reply to #3 )

 
If this works, I will be one happy person.

I am having our Perl guru try it now.

One question: we have shell scripts calling a whole bunch of perl scripts, and it is one of those shell scripts I want to launch.

Where should I do this magic to affect everything that needs to be affected?

Scott
 
This topic has 4 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 133

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums