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.