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 3445010
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:59:17+00:00 2026-05-18T08:59:17+00:00

I use Runtime exec() method to create a subprocess in Java. However, since the

  • 0

I use Runtime exec() method to create a subprocess in Java. However, since the subprocess is an interactive program, I need to provide input to it as and when required by it. Also I need to show the output of the subprocess. How can I do this in the simplest possible way?

I was using a StreamGobbler to show the program output using process.getInputStream(). I, however, do not know how to identify when the program is waiting for input and when to provide it input using proc.getOutputStream. How can I do this?

  • 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-18T08:59:17+00:00Added an answer on May 18, 2026 at 8:59 am

    You need to copy the input and output between the subprocess’ streams and System streams (System.in, System.out and System.err). This is related to my recent quesion. The best solution I have found so far is:

    import java.io.FileInputStream;
    import java.io.FilterInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.lang.reflect.Field;
    import java.nio.ByteBuffer;
    import java.nio.channels.AsynchronousCloseException;
    import java.nio.channels.FileChannel;
    
    class StreamCopier implements Runnable {
        private InputStream in;
        private OutputStream out;
    
        public StreamCopier(InputStream in, OutputStream out) {
            this.in = in;
            this.out = out;
        }
    
        public void run() {
            try {
                int n;
                byte[] buffer = new byte[4096];
                while ((n = in.read(buffer)) != -1) {
                    out.write(buffer, 0, n);
                    out.flush();
                }
            }
            catch (IOException e) {
                System.out.println(e);
            }
        }
    }
    
    class InputCopier implements Runnable {
        private FileChannel in;
        private OutputStream out;
    
        public InputCopier(FileChannel in, OutputStream out) {
            this.in = in;
            this.out = out;
        }
    
        public void run() {
            try {
                int n;
                ByteBuffer buffer = ByteBuffer.allocate(4096);
                while ((n = in.read(buffer)) != -1) {
                    out.write(buffer.array(), 0, n);
                    out.flush();
                }
                out.close();
            }
            catch (AsynchronousCloseException e) {}
            catch (IOException e) {
                System.out.println(e);
            }
        }
    }
    
    public class Test {
        private static FileChannel getChannel(InputStream in)
                throws NoSuchFieldException, IllegalAccessException {
            Field f = FilterInputStream.class.getDeclaredField("in");
            f.setAccessible(true);
            while (in instanceof FilterInputStream)
                in = (InputStream)f.get((FilterInputStream)in);
            return ((FileInputStream)in).getChannel();
        }
    
        public static void main(String[] args)
                throws IOException, InterruptedException,
                       NoSuchFieldException, IllegalAccessException {
            Process process = Runtime.getRuntime().exec("sh -i +m");
            Thread outThread = new Thread(new StreamCopier(
                    process.getInputStream(), System.out));
            outThread.start();
            Thread errThread = new Thread(new StreamCopier(
                    process.getErrorStream(), System.err));
            errThread.start();
            Thread inThread = new Thread(new InputCopier(
                    getChannel(System.in), process.getOutputStream()));
            inThread.start();
            process.waitFor();
            System.in.close();
            outThread.join();
            errThread.join();
            inThread.join();
        }
    }
    

    The tricky part here is to extract a channel from System.in. Without this you will not be able to interrupt the thread that reads input when the subprocess terminates.

    This approach has a serious drawback: after closing System.in you can no longer read from it. The workaround that I’m currently using is to have a single input redirecting thread used for all subprocesses.

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

Sidebar

Related Questions

I am trying to use Runtime exec() to run a vba script with arguements.
I wish to use the method with the following signature: exec(String command, String[] envp,
I want to use a linux command line tool from my Java program. I
I've just installed PowerBuilder 11.5. I'm trying to use the PowerBuilder Runtime Packager that
As I use the web, I regularly get runtime errors (usually javascript) being reported
I want to launch a file(a document) from a Java program and phase the
I would like to spawn a subprocess Java Virtual Machine (through a Java API
My application calls Runtime.exec() to launch an executable in a separate process at start
im trying to do this on Android: Process p = Runtime.getRuntime().exec(sh); DataOutputStream out =
I need to write a function which could create any type object. It receives

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.