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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:10:52+00:00 2026-05-25T19:10:52+00:00

I already have a java program using log4j to write output on console, while

  • 0

I already have a java program using log4j to write output on console, while now I want use another java program to invoke the first one(sub-process) and intercept its output.
I use the method in this page http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4, some codes like below

class StreamRedirector extends Thread
{
InputStream is;

StreamRedirector(InputStream is)
{
    this.is = is;
}

public void run()
{
    try
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line=null;
        while ( (line = br.readLine()) != null)
        {
            System.out.println(line);
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
}
.............
try
    {
        Process p = Runtime.getRuntime().exec("run.bat");

        StreamRedirector errSR = new StreamRedirector(p.getErrorStream());

        StreamRedirector outSR = new StreamRedirector(p.getInputStream());

        errSR.run();
        outSR.run();

        int exitVal = p.waitFor();
        System.out.println("Exit Value: " + exitVal);
    }

log4j config:
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">

    <param name="Target" value="System.out" />

    <layout class="org.apache.log4j.PatternLayout">

        <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p  [%t] [%c{1}] %m%n" />
    </layout>
</appender>

I can get the output message, but just in “run.bat” using echo XXX , not the message generated by log4j. If I run “run.bat”, I can see both the output in console. Is there a way that I can also get the log4j output in parent process? thanks.

  • 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-25T19:10:53+00:00Added an answer on May 25, 2026 at 7:10 pm

    The StreamRedirector extends Thread but you call run not start. This means that run is getting called synchronously, in the calling thread. Which means that in the following

        errSR.run();
        outSR.run();
    

    The second line does not execute until the first completes. I guess if the error stream does not produce much output then it will eventually complete. But is that really what you intend to do? Instead try

        errSR.start();
        outSR.start();
    

    If the program is using log4j I urge you to consider SocketAppender to grab what is written to it from a different process.

    EIDT: Another approach is for StreamRedirector to extend Runnable instead of Thread. The calling code then changes to

    Thread errorThread = new Thread(new StreamRedirector(p.getErrorStream()));
    ....
    errorThread.start();
    

    This won’t prevent you from calling errorThread.run() but if you do call it then it will return immediately (instead of blocking as it currently does) suggesting that something is wrong.

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

Sidebar

Related Questions

Using google's Protocul Buffers , I have a service already written in Java which
I want to run a java program using shell script. The java program is
I use Java. I already have a class for a custom object called Subject.
I want to call a C subroutine from Java. I'm using JNI. I have
i have already installed tomcat service with old tomcat path.. Now i want to
I want to assign a resource I already have a second name, similar to
For statistical reasons, I want an extensive analysis from a dataset. I already have
I have my JAVA_HOME set to: C:\Program Files (x86)\Java\jdk1.6.0_18 After I run maven install
I have a java program that connects to a MySql database and it's working
String fileToBeRead = C:/Documents and Settings/Developer/Desktop/Anand exmps/Anand.xls; I have completed a java program in

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.