• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • 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.

  • 2
  • 2
1 Solution
exec() isn't a shell so things like piping won't work.
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?
Following example shows how to read stdout/stderr

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:
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);
PAQed with points refunded (125)

Community Support Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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