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

  • SEARCH
  • Home
  • 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 7829519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:48:30+00:00 2026-06-02T10:48:30+00:00

Just assume there is a program which takes inputs from the standard input. For

  • 0

Just assume there is a program which takes inputs from the standard input.

For example:

cin>>id;

What I want to figure out is how to execute the process and give some input to its standard input. Getting the output of the process is not an issue for me. It works properly. The question is how to feed inputs for such processes using java.lang.Process class.

If there are any other third party libraries like Apache commons please mention them also.

Thanks in advance!

  • 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-06-02T10:48:31+00:00Added an answer on June 2, 2026 at 10:48 am

    You need to start a separate thread which reads from the output of one process and writes it as input to the other process.

    Something like this should do:

    class DataForwarder extends Thread {
    
        OutputStream out;
        InputStream in;
    
        public DataForwarder(InputStream in, OutputStream out) {
            this.out = out;
            this.in = in;
        }
    
        @Override
        public void run() {
            byte[] buf = new byte[1024];
            System.out.println("Hej");
            try {
                int n;
                while (-1 != (n = in.read(buf)))
                    out.write(buf, 0, n);
                out.close();
            } catch (IOException e) {
                // Handle in some suitable way.
            }
        }
    }
    

    Which would be used for prod >> cons as follows:

    class Test {
        public static void main(String[] args) throws IOException {
    
            Process prod = new ProcessBuilder("ls").start();
            Process cons = new ProcessBuilder("cat").start();
    
            // Start feeding cons with output from prod.
            new DataForwarder(prod.getInputStream(), cons.getOutputStream()).start();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I assume there must be a system and language independent way to just stick
I assume this just returns an int. Is there anything else going on I
I have a fairly intensive application, which I've just converted from a console application
Hey there, I want to evaluate a mathematical function (user-defined) which returns several values
I had assume I could just do this, but I don't have a way
Assume a typical, database and session driven ASP application developed using best practices just
Well it's late so I assume I'm just exhausted but I've been trying to
FieldInfo has an IsStatic member, but PropertyInfo doesn't. I assume I'm just overlooking what
I just realized something crazy, which I assumed to be completely impossible : when
I have a Python program that takes around 10 minutes to execute. So I

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.