File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Problem calling piped perl routine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem calling piped perl routine" Watch "Problem calling piped perl routine" New topic
Author

Problem calling piped perl routine

Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 230

I am calling few perl routines from my Java program (in an Unix environment) using Runtime process call. While most get executed normally, I am facing problem with routines that get parameters as output from other routine i.e. routines that use Piping, ex:

When run directly from command line it works well. But when called through my program only the routine1 gets exceuted printing its output at command line, the second routine doesn't get the output from first and does not execute. I am not aware of and cannot change the perl routines.

How do you call piped Perl routines using Runtime process call?

Thanks for your time,
[ May 12, 2005: Message edited by: Debashish Chakrabarty ]

Debashish
SCJP2, SCWCD 1.4, PMP, ITIL Foundation
Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
You might need to call a shellscript that calls the chain of perl scripts. Otherwise your pipe will break.

Other approach would be to store output of script 1 and to pass it to script 2 on a subsequent call.

Man but this sounds nasty, phew

J.
Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 230

Passing the Standard output from the Process object was a thing I had in mind. But I am not at all sure how would I pass this stream to the second routine.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Implementing the piping yourself would be interesting. Start two processes, start a thread that reads the stdout of the first and writes to the stdin of the second. It would sure be nicer if you could get the piping going outside of Java, tho.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 230

I finally found the solution here. As the article says, Runtime.exec invokes actual executable binary programs. Syntax such as pipe and > are part of a particular command processor, and are only understood by that processor. So the command preceding the pipe is executed, but the rest of the shell command is not.

Invoking the process as follows works:


Do let me know if this understanding is flawed.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Makes perfect sense. I'm glad you found it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem calling piped perl routine