Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem calling piped perl routine

 
Debashish Chakrabarty
Ranch Hand
Posts: 231
Firefox Browser Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
Firefox Browser Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Debashish Chakrabarty
Ranch Hand
Posts: 231
Firefox Browser Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Makes perfect sense. I'm glad you found it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic