0

I defined a function to return the command process output, in windows its working fine, but in Linux its failing.

Fro command ps -u root|grep java|awk '{print $1}' its always returning null

Even before executing this method I tried execution of command in command prompt, there its successfully went

#ps -u root|grep java|awk '{print $1}'

385

2018

2048

4242

21290

25110

25589

26166

/**
 * This method will execute and returns the command output
 * @param commandToBeExecute
 * @return
 */
protected String getCMDOutput(final String commandToBeExecute)
{
    String cmdOutput = "";
    InputStream inputstream = null;
    InputStreamReader inputstreamreader = null;
    try 
    {
        final Process process = runTime.exec(commandToBeExecute);
        inputstream = process.getInputStream();
        inputstreamreader = new InputStreamReader(inputstream);
        bufferedReader = new BufferedReader(inputstreamreader);
        String currentLine;
        currentLine = bufferedReader.readLine();\\Always returning null for **ps -u root|grep java|awk '{print $1}'** command
        while(currentLine!=null)
        {
            cmdOutput += currentLine;
            currentLine = bufferedReader.readLine();
        }
    } 
    catch (IOException ioException) {
        logger.error(ioException.getMessage(), ioException);
    }
    finally
    {
        try
        {
            if(bufferedReader!=null)
            {
                bufferedReader.close();
            }
            if(inputstream!=null)
            {
                inputstream.close();
            }
            if(inputstreamreader!=null)
            {
                inputstreamreader.close();
            }           
        } 
        catch (IOException ioException) {
            logger.error(ioException.getMessage(), ioException);
        }
    }
    return cmdOutput;
}

When I tried the below code its giving a error

inputstream = process.getErrorStream();
        inputstreamreader = new InputStreamReader(inputstream);
        bufferedReader = new BufferedReader(inputstreamreader);
        currentLine = bufferedReader.readLine();

 (java.lang.String) ERROR: User name does not exist.

So, what would be the reason for this error, please guide to solve to solve this problem.

kgopi
  • 141
  • 1
  • 8
  • 18
  • If it running in windows I think the problem is related to permissions in linux or you may have to be a super user for executing the command. Are yyou logged in with a super user account. – Nikhil Agrawal Apr 12 '13 at 09:09
  • Yes, what ever the user **root** I used in the command, with the same user I logged in. – kgopi Apr 12 '13 at 09:21
  • The command which you are executing is installed in your system or not. Just check for it in the terminal. – Nikhil Agrawal Apr 12 '13 at 09:25
  • I already specified, I able to ran the command successfully from command prompt see the information provided above(from that same machine only I am running the java program through eclipse) – kgopi Apr 12 '13 at 09:29
  • It may be due to the pipes in your command, take a look at this [answer](http://stackoverflow.com/a/5928316/69875). – Jonathan Apr 12 '13 at 09:37

1 Answers1

0
currentLine = bufferedReader.readLine();\\Always returning null for **ps -u root|grep java|awk '{print $1}'** command
        while(currentLine!=null)
        {
            cmdOutput += currentLine;
            currentLine = bufferedReader.readLine();
        }

Change above code to :And try once.

    while((currentLine = bufferedReader.readLine())!=null)
    {
        cmdOutput += currentLine;
        //currentLine = bufferedReader.readLine();
    }
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
  • Thanks for the reply, I edited my description please comment on **When I tried the below code its giving a error** column – kgopi Apr 12 '13 at 09:11