[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Problem calling perl routine

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, ex:

[CODE] ./routine2.pl param1 | ./routine1.pl -directory  param2 [/CODE]

When run directly from command line it works well. But when called through my program it behaves randomly sometimes outputting the routine code other times outputting the file content being read by one of the routine. I am not aware of and cannot change the perl routines.

Thanks,
0
debuchakrabarty
Asked:
debuchakrabarty
  • 2
  • 2
1 Solution
 
objectsCommented:
exec() isn't a shell so things like piping won't work.
0
 
debuchakrabartyAuthor Commented:
Is there any way I could use the Error/Standard output streams from the Runtime Process call.

How can supply these streams output by routine2 to routine1.pl then? Any clue? Has anybody done this before?
0
 
objectsCommented:
Following example shows how to read stdout/stderr

http://www.objects.com.au/java/examples/util/ConsoleExec.do
0
 
debuchakrabartyAuthor Commented:
I finally found the solution at http://java.sun.com/developer/JDCTechTips/2003/tt0304.html.

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:
[CODE]  
String param = "./routine1.pl param1 | ./routine2.pl -directory  param2";
Runtime runtime = Runtime.getRuntime();
String[] args = new String[]{"sh", "-c", param};
Process p = runtime.exec(args);
[/CODE]
0
 
moduloCommented:
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now