Problem calling perl routine

Posted on 2005-05-11
Last Modified: 2010-08-05
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] ./ param1 | ./ -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.

Question by:debuchakrabarty
    LVL 92

    Expert Comment

    exec() isn't a shell so things like piping won't work.

    Author Comment

    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 then? Any clue? Has anybody done this before?
    LVL 92

    Expert Comment

    Following example shows how to read stdout/stderr

    Author Comment

    I finally found the solution at

    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 = "./ param1 | ./ -directory  param2";
    Runtime runtime = Runtime.getRuntime();
    String[] args = new String[]{"sh", "-c", param};
    Process p = runtime.exec(args);

    Accepted Solution

    PAQed with points refunded (125)

    Community Support Moderator

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Java MSI Solution 3 53
    twoTwo  challenge 35 74
    userCompare  challenge 3 55
    wordcount challenge 11 51
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    The viewer will learn how to implement Singleton Design Pattern in Java.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now