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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:44:18+00:00 2026-05-16T22:44:18+00:00

I have written the java code below, which executes another java program named Newsworthy_RB.

  • 0

I have written the java code below, which executes another java program named “Newsworthy_RB”.

Newsworthy_RB.java contains both the System.out.printlln() and System.err.println() statements.

I want both the outputs to be printed in the command prompt console.

What has to be done inorder to obtain the same.

The below program just prints the out.println() statements and not the err.println() statements.

Kindly let me know whether the code below will function as i expect?

command = "java -cp .:../sqljdbc.jar SetHash Newsworthy_RB";
Process child1 = Runtime.getRuntime().exec(command);
InputStream in1 = child1.getErrorStream();
InputStream in2 = child2.getInputStream();
while ((c = in1.read()) != -1 || (c = in2.read()) != -1) {
        System.out.print((char)c);
    }
  • 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-16T22:44:19+00:00Added an answer on May 16, 2026 at 10:44 pm

    First of all, the preferred way of starting external programs is through ProcessBuilder. It is even mentioned in the docs for Runtime:

    ProcessBuilder.start() is now the preferred way to start a process with a modified environment.

    In ProcessBuilder you have a very convenient method called redirectErrorStream:

    Sets this process builder’s redirectErrorStream property.

    If this property is true, then any error output generated by subprocesses subsequently started by this object’s start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() method. This makes it easier to correlate error messages with the corresponding output. The initial value is false.

    A complete example of how to output both standard error and standard out:

    import java.io.*;
    
    public class Test {
        public static void main(String... args) throws IOException {
    
            ProcessBuilder pb =
                    new ProcessBuilder("java", "-cp", "yourClassPath", "HelloWorld");
    
            pb.redirectErrorStream(true);
            Process proc = pb.start();
    
            Reader reader = new InputStreamReader(proc.getInputStream());
            int ch;
            while ((ch = reader.read()) != -1)
                System.out.print((char) ch);
            reader.close();
        }
    }
    

    Response to your update: No, the code with

    while ((c = in1.read()) != -1 || (c = in2.read()) != -1)
    

    will not work, since read() is a blocking method and you only have one thread. Your only option is to use one thread per input-stream, or, (preferrably) merge the two input-streams into one, using ProcessBuilder.redirectErrorStream.

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

Sidebar

Related Questions

I have written a java program named Automate.java, in which the another java program
I have written a MapReduce program, code is below: import java.io.IOException; import java.util.Iterator; import
I have written a multi threaded java code, which when runs creates 8 threads
Let's assume I have a framework written in Java and some C++ code which
I have written the below code which has a text-field and a button. as
I have written java code for generating json of my searched data from file.But
I have written code in Java to access web cam,and to save image... I
In our application, I have seen code written like this: User.java (User entity) public
I have written a java program generates lots of files (say txt files) in
I have written a java program that is actually works as a gui to

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.