Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7031533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:48:56+00:00 2026-05-28T00:48:56+00:00

Is it possible to pass the output of one process created by ProcessBuilder to

  • 0

Is it possible to pass the output of one process created by ProcessBuilder to another process created by another ProcessBuilder? For example, if I’m trying to execute this shell command:

ls | grep build.xml

how should I do it with ProcessBuilder?

as @erdinc suggested, I tried this:

Process process = Runtime.getRuntime().exec("ls");
InputStream is = process.getInputStream();
byte[] buf = new byte[1000];
is.read(buf);
String parameter = new String(buf);
System.out.println("grep build " + parameter);

Process proc2 = Runtime.getRuntime().exec("grep build " + parameter);
InputStream is2 = proc2.getInputStream();
byte[] buf2 = new byte[1000];
is2.read(buf2);
String result = new String(buf2);
System.out.println("proc2 result: " + result);

but it produces different result compare to when I run the script directly in the shell. Where did I do wrong?

Solved: Please see Philipp Wendler solution

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:48:56+00:00Added an answer on May 28, 2026 at 12:48 am

    The problem with the solution you have is that it reads only the first 1000 bytes from the output of ls and passes them to grep. As you don’t know the amount of output from ls before, you need to read it iteratively until it is exhausted.

    Here is a solution using BufferedReader, which will send each line of output to grep:

    Process lsProcess = Runtime.getRuntime().exec("ls");
    BufferedReader lsOutput = new BufferedReader(new InputStreamReader(lsProcess.getInputStream()));
    Process grepProcess = Runtime.getRuntime().exec("grep build.xml");
    BufferedWriter grepInput = new BufferedWriter(new OutputStreamWriter(grepProcess.getOutputStream()));
    
    String line;
    // read each line from ls until there are no more
    while ((line = lsOutput.readLine()) != null) {
        // and send them to grep
        grepInput.write(line);
        grepInput.newLine();
    }
    
    // send end-of-file signal to grep so it will terminate itself
    grepInput.close();
    

    Of course you need to add the appropriate error-handling here.
    You can omit the Reader and Writer and pass byte[] arrays directly from the input to the output stream if this solution is too slow.

    However, a much better solution would be not to use ls and grep to look for a file in the filesystem, but to use the appropriate Java API. For example if you create a File object for the directory you are interested in, you can use the various listFiles methods on it to list all files in this directory. This method is much easier, portable, less error-prone and probably faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to pass a reference to a function to another function in
Is it possible to pass command line arguments into a function from within a
I want to pass some variables from one class to another, but the code
I've got a Perl script that needs to execute another Perl script. This second
Possible Duplicate: Pass a data.frame column name to a function I am trying to
I'd like to pass the output of an included Twig template to another included
Possible Duplicate: Pass a PHP string to a Javascript variable (and escape newlines) I
Is it possible to pass a function/callback from javascript to a java applet? For
Is it possible to pass a App setting string in the web.config to a
Is it possible to pass a path such as subject/name to a template then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.