Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Redirect error and output stream using java program

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Redirect error and output stream using java program

    I'm facing following problem:

    1. I have a Java program which calls other java program.

    2. I call the other java program by:

    ---> I run the batch file.
    Process proc =Runtime.getRuntime().exec(batchFile);
    ---> This batch file calls the java program.

    Now In my main application that is main java program I'm capturing error and output stream of DOS command
    line so that it will be displayed by my swing application.

    proc.getInputStream() and proc.getErrorStream().

    But I'm not able to capture the error stream

    If I don't capture the streams and run command window it will display error as
    well as o/p on command window.

    I'm not getting what is going wrong over here.

    Thanks in advance.

    Lee


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Redirect error and output stream using java program

    try using redirectErrorStream(true); so that you can read it using getInputStream()

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Redirect error and output stream using java program

    Hi,

    Thanks for the reply.

    I cannot do this for following two reasons:
    1. I want to capture error streams and o/p streams differently as they are treated differently by my swing application.

    2. It is not that I'm not able to catch the error stream completely.
    The java class which I'm calling through the batch file has the main() method. If I throw some exception in that class and I catch it. It will be redirected to the Error stream.

    But If it is being thrown from some other class then it cannot be captured by error stream.

    Initially I thought it is due to the fact that may be log4j is redirecting it to some other place. But thats not true if I run this through command line I can view all error and o/p msg.

    Lee

  4. #4
    Junior Member
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Redirect error and output stream using java program

    I'm facing following problem:

    1. I have a Java program which calls other java program.

    2. I call the other java program by:

    ---> I run the batch file.
    Process proc =Runtime.getRuntime().exec(batchFile);
    ---> This batch file calls the java program.

    Now In my main application that is main java program I'm capturing error and output stream of DOS command
    line so that it will be displayed by my swing application.

    proc.getInputStream() and proc.getErrorStream().

    But I'm not able to capture the error stream

    If I don't capture the streams and run command window it will display error as
    well as o/p on command window.

    I'm not getting what is going wrong over here.

    Idont want to use try redirectErrorStream(true); or following two reasons:
    1. I want to capture error streams and o/p streams differently as they are treated differently by my swing application.

    2. It is not that I'm not able to catch the error stream completely.
    The java class which I'm calling through the batch file has the main() method. If I throw some exception in that class and I catch it. It will be redirected to the Error stream.

    But If it is being thrown from some other class then it cannot be captured by error stream.

    Initially I thought it is due to the fact that may be log4j is redirecting it to some other place. But thats not true if I run this through command line I can view all error and o/p msg.

    Lee

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Redirect error and output stream using java program

    Hi,

    This is solved. We need to read input and error stream in two different threads to avoid the blocking.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Thumbs up Re: Redirect error and output stream using java program

    Quote Originally Posted by leenabora View Post
    Hi,

    This is solved. We need to read input and error stream in two different threads to avoid the blocking.
    I'm glad you resolved your problem leenabora.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Replies: 3
    Last Post: April 26th, 2011, 02:51 AM
  2. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM
  3. Replies: 4
    Last Post: April 29th, 2009, 10:53 AM
  4. Display output from a file using regular expression
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 29th, 2008, 09:33 AM
  5. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 10:42 AM