user1608896
user1608896

Reputation: 9

BufferedReader not working as expected

The below code is not getting executed completely after this line " bufferedReader.readLine(); ". The Program works fine when i execute the system command with out mentioning IPAddress of the remote PC.

class Test    
{       
public static void main(String arg[])    
{    
     Process p;    
     Runtime runTime;    
     String process = null;    
     try {    
        runTime = Runtime.getRuntime();    
        p = runTime.exec("sc \\xx.xx.xx.xx query gpsvc");      // For Windows    
        InputStream inputStream = p.getInputStream();    
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);    
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);    
        String line = bufferedReader.readLine();    
        process = "&";    
        while (line != null) {    
            line = bufferedReader.readLine();    
            process += line + "&";    
        }    
        StringTokenizer st = new StringTokenizer(proc, "&");    
        System.out.println("token size "+st.countTokens());    
        while (st.hasMoreTokens()) {        
            String testData = st.nextToken();    
        bufferedReader.close();    
        inputStreamReader.close();    
        inputStream.close();    
        }    

     } catch (IOException e) {    
       System.out.println("Exception arise during the read Processes");    
       e.printStackTrace();    
     }    

}    
}        

Upvotes: 1

Views: 269

Answers (1)

Jiji
Jiji

Reputation: 383

Check your command inside exec method

p = runTime.exec("sc \\xx.xx.xx.xx query gpsvc");

The syntax is wrong here and if you execute this from command prompt, you will be prompted with the below question.

Would you like to see help for the QUERY and QUERYEX commands? [ y | n ]:

And the program wouldn't return until you enter y or n. Since the program is not terminating, you wouldn't be able to read the console output and that's the reason your program is getting stuck on String line = bufferedReader.readLine();

Upvotes: 1

Related Questions