コマンド プロセスの出力を返す関数を定義しました。Windows では正常に動作しますが、Linux では失敗します。
コマンドps -u root|grep java|awk '{print $1}'から常にnullを返す
このメソッドを実行する前でも、コマンドプロンプトでコマンドを実行しようとしましたが、成功しました
#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;
}
以下のコードを試してみると、エラーが発生しました
inputstream = process.getErrorStream();
inputstreamreader = new InputStreamReader(inputstream);
bufferedReader = new BufferedReader(inputstreamreader);
currentLine = bufferedReader.readLine();
(java.lang.String) ERROR: User name does not exist.
では、このエラーの理由は何でしょうか。この問題を解決するためのガイドをお願いします。