Obtaining process output with Java doesn't display all the content -


i executing processes java , parsing results. however, when read output don't content.

for example, if execute windows console:

cmd /c tasklist | find "java" 

it shows:

javaw.exe    6192 console     1   683.668 kb java.exe     8448 console     1    35.712 kb java.exe     7252 console     1    35.736 kb javaw.exe    3260 console     1    76.652 kb java.exe     9728 console     1    35.532 kb 

but if same java process 2 of them appear:

java.exe     8448 console     1    35.712 kb javaw.exe    3260 console     1    76.652 kb 

this simplified version of code:

public static void printpidsofjavaprocesses() {     try {         string[] params = null;         if (isinwindows()) {             params = new string[6];             params[0] = "cmd";             params[1] = "/c";             params[2] = "tasklist";             params[3] = "|";             params[4] = "find";             params[5] = "\"java\"";         } else {             ...         }          process process = processutil.executeconsolecommand(params);         printconsoleoutput(process)      } catch (ioexception e) {         log.error("it not possible obtain pids of active processes");     } }  public static void printconsoleoutput(process process) {     inputstream input = process.getinputstream();     bufferedreader reader = new bufferedreader(new inputstreamreader(input));      try {         string line = null;         while ((line = reader.readline()) != null) {             system.out.println(line);         }     } catch (ioexception e) {         log.error("it not possible obtain process output", e);     } }  public static process executeconsolecommand(string[] params) throws ioexception {      // create process     final processbuilder processbuilder = new processbuilder(params);      // redirect errors avoid deadlocks when buffer full     processbuilder.redirecterrorstream(true);      // launch process     process process = processbuilder.start();      return process; } 

i have searched here , on google , people launch , read processes in similar way, cause of problem might related console command. have idea? in advance

i have solved using wmic instead of tasklist. code:

public static list<string> getpidsofjavaprocesses() throws processexception {      list<string> pids = null;     boolean isonwindows = isonwindows();      if (isonwindows) {         string[] commandparams = getcommandparamsforgettingprocessesonwindows();         process pr = executeconsolecommand(commandparams);         pids = getconsoleoutput(pr);     } else {         string[] commandparams = getcommandparamsforgettingprocessesonlinux();         process pr = executeconsolecommand(commandparams);         list<string> outputlines = getconsoleoutput(pr);         pids = getjavapidsfromconsoleoutputonlinux(outputlines);     }      return pids; }  public static boolean isonwindows() {     boolean isonwindows = false;     string osname = system.getproperty("os.name");     if (osname.tolowercase().contains("windows")) {         isonwindows = true;     }      return isonwindows; }  private static string[] getcommandparamsforgettingprocessesonlinux() {      string[] params = new string[3];     params[0] = "bash";     params[1] = "-c";     params[2] = "ps -fe | grep \"java\"";      return params; }  private static string[] getcommandparamsforgettingprocessesonwindows() {      string[] params = new string[11];     params[0] = "cmd";     params[1] = "/c";     params[2] = "wmic";     params[3] = "process";     params[4] = "where";     params[5] = "name=\"javaw.exe\"";     params[6] = "get";     params[7] = "processid";     params[8] = "|";     params[9] = "more";     params[10] = "+1";      return params; }  public static process executeconsolecommand(string[] params) throws processexception {      // create process     final processbuilder processbuilder = new processbuilder(params);      // redirect errors avoid deadlocks when buffers full.     processbuilder.redirecterrorstream(true);      // launch process     process process = null;     try {         process = processbuilder.start();     } catch (ioexception e) {         log.error("exception launching process", e);         throw new processexception("exception launching process", e);     }      return process; }  public static list<string> getconsoleoutput(process process) throws processexception {     list<string> lines = new arraylist<string>();     inputstream input = process.getinputstream();     bufferedreader reader = new bufferedreader(new inputstreamreader(input));      try {         string line = reader.readline();         while (line != null) {             line = line.trim();             if (!line.isempty()) {                 lines.add(line);             }             line = reader.readline();         }     } catch (ioexception e) {         log.error("it not possible obtain process output", e);         throw new processexception("it not possible obtain process output", e);     } {         try {             if (input != null) {                 input.close();             }             if (reader != null) {                 reader.close();             }         } catch (ioexception e) {             log.error("it not possible close console stream", e);             throw new processexception("it not possible close console stream", e);         }     }      return lines; } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

angularjs - ng-repeat duplicating items after page reload -