java - How can't show if service running test green -
i have 5 services in file.bat
3 running , 2 stopped. want have 3 success green test , 2 red
i think problem var ligne
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; public class cmd { static string = null; static string ligne = ""; public final string output = null; static string chemin = "vvvvvvvvvv"; private static bufferedreader getoutput(process p) { return new bufferedreader(new inputstreamreader(p.getinputstream())); } private static bufferedreader geterror(process p) { return new bufferedreader(new inputstreamreader(p.geterrorstream())); } static public string stateprocess() { if (ligne.contains("running")) { = "running"; system.out.println("ok"); } else { = "stopped"; system.out.println("no"); } return a; } public boolean ping(string chemin) { int exitvalue = 1; try { string[] commande = {"cmd.exe", "/c", chemin + "test-sc.bat"}; process p = runtime.getruntime() .exec(commande); bufferedreader output = getoutput(p); bufferedreader error = geterror(p); while ((ligne = output.readline()) != null) { system.out.println(ligne); // system.out.println(ligne.contains("stopped")); } while ((ligne = error.readline()) != null) { system.out.println(ligne); } exitvalue = p.waitfor(); } catch (ioexception e) { e.printstacktrace(); } catch (interruptedexception e) { e.printstacktrace(); } return exitvalue == 0; } public static void main(string[] args) { cmd exemple = new cmd(); boolean trouve = exemple.ping(chemin); system.out.println(trouve); } }
this code stay reading out , err streams until end of streams has been reached. let java code wait till process has completed task.
so if want, suggest move statement
exitvalue = p.waitfor();
before 2 while loops read streams.
this should let sure both streams written completely.
Comments
Post a Comment