java - Executing 'adb logcat' command using Runtime class -
i trying logcat content jtextpane. used following code hoping return content string freeze , also, doesn't produce error.
process exec = null; try { exec = runtime.getruntime().exec("adb logcat -d"); inputstream errorstream = exec.geterrorstream(); bufferedreader ebr = new bufferedreader(new inputstreamreader(errorstream)); string errorline; while ((errorline = ebr.readline()) != null) { system.out.println("[error] :- " + errorline); } if (exec.waitfor() == 0) { inputstream infostream = exec.getinputstream(); inputstreamreader isr = new inputstreamreader(infostream); bufferedreader ibr = new bufferedreader(isr); string infoline; while ((infoline = ibr.readline()) != null) { system.out.println("[info] :- " + infoline); } } } catch (ioexception | interruptedexception ex) { ex.printstacktrace(); } { if (exec != null) { exec.destroy(); } }
i referred tutorials but, not filling problem. wrong? there other methods logcat content string programmatically? sorry if dumb question.
the issue you're seeing you're trying process command streams , wait executing process, in same thread. it's blocking because process reading streams waiting on process , you're losing stream input.
what you'll want implement function reads/processes command output (input stream) in thread , kick off thread when start process.
second, you'll want use processbuilder
rather runtime.exec
.
something can adapted want:
public class test { public static void main(string[] args) throws exception { string startdir = system.getproperty("user.dir"); // start in current dir (change if needed) processbuilder pb = new processbuilder("adb","logcat","-d"); pb.directory(new file(startdir)); // start directory pb.redirecterrorstream(true); // redirect error stream stdout process p = pb.start(); // start process // start new thread handle stream input new thread(new processtestrunnable(p)).start(); p.waitfor(); // wait if needed } // mimics stream gobbler, allows user process result static class processtestrunnable implements runnable { process p; bufferedreader br; processtestrunnable(process p) { this.p = p; } public void run() { try { inputstreamreader isr = new inputstreamreader(p.getinputstream()); br = new bufferedreader(isr); string line = null; while ((line = br.readline()) != null) { // output here... } } catch (ioexception ex) { ex.printstacktrace(); } } } }
Comments
Post a Comment