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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:17:35+00:00 2026-06-13T09:17:35+00:00

I am executing a command which returns me the Revision number of a file;

  • 0

I am executing a command which returns me the Revision number of a file; ‘fileName’. But if there is some problem executing the command, then the application hangs up. What can I do to avoid that condition? Please find below my code.

String cmd= "cmd /C si viewhistory --fields=revision --project="+fileName; 
Process p = Runtime.getRuntime().exec(cmd) ;  
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));  
String line = null; 
while ((line = in.readLine()) != null) {  
System.out.println(line);  
} 

} catch (Exception e) {  
e.printStackTrace();  
 }
  • 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-13T09:17:36+00:00Added an answer on June 13, 2026 at 9:17 am

    I guess the issue is that you are only reading InputStream and not reading ErrorStream. You also have to take care that both the streams are read in parallel. It may so happen that currently the data piped from the output stream fills up the OS buffer, your exec command will be automatically be suspended to give your reader a chance to empty the buffer. But the program will still be waiting for the output to process. Hence, the hang occurs.

    You can create a separate class to handle both the Input and Error Stream as follows,

    public class ReadStream implements Runnable {
        String name;
        InputStream is;
        Thread thread;      
        public ReadStream(String name, InputStream is) {
            this.name = name;
            this.is = is;
        }       
        public void start () {
            thread = new Thread (this);
            thread.start ();
        }       
        public void run () {
            try {
                InputStreamReader isr = new InputStreamReader (is);
                BufferedReader br = new BufferedReader (isr);   
                while (true) {
                    String s = br.readLine ();
                    if (s == null) break;
                    System.out.println ("[" + name + "] " + s);
                }
                is.close ();    
            } catch (Exception ex) {
                System.out.println ("Problem reading stream " + name + "... :" + ex);
                ex.printStackTrace ();
            }
        }
    }
    

    The way you use it is as follows,

    String cmd= "cmd /C si viewhistory --fields=revision --project="+fileName; 
    Process p = Runtime.getRuntime().exec(cmd) ;  
    s1 = new ReadStream("stdin", p.getInputStream ());
    s2 = new ReadStream("stderr", p.getErrorStream ());
    s1.start ();
    s2.start ();
    p.waitFor();        
    } catch (Exception e) {  
    e.printStackTrace();  
    } finally {
        if(p != null)
            p.destroy();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m executing the following command grep bruno < bash.txt which gives me the right
I have this query which on executing in my sql command line client executes
Say I have a command line C program which is currently executing, and I
I'm currently facing a weird problem while executing a command from my bash script.
I know that SqlContext.WindowsIdentity returns the current user that is executing the command (in
I have a tcl code which invokes ping command and returns its response ,
I am creating a Python script within which I am executing UNIX system commands.
Why after executing the command openssl sha1 < /dev/null | wc --bytes the output
I have an awesome trouble with Gem. After executing this command: rm -f /usr/local/lib/ruby/gems/1.9.1/cache/*
I am executing a unix command through perl script to get size of a

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.