Java: Read data from the terminal using Process() -
i'm trying read response terminal while using command in process()
function. have looked on other examples can't work. wan't able process data , use progressbar example, need access data when appears in terminal. in code i'm using textarea see if works now. can see data in console in eclipse nothing appears in textarea. doing wrong?
public void unpack(string filepath, string directory) { processbuilder pb = new processbuilder("/usr/local/bin/unrar", "x", "-y",filepath); pb.inheritio(); pb.directory(new file(directory)); try { final process p = pb.start(); bufferedreader in = new bufferedreader( new inputstreamreader(p.getinputstream())); string line = null; while ((line = in.readline()) != null) { textarea.settext(line); } try { int exitval = p.waitfor(); } catch (interruptedexception e) { e.printstacktrace(); } } catch (ioexception e) { e.printstacktrace(); } }
the response in mac terminal when running command myself result is:
unrar 4.20 freeware copyright (c) 1993-2012 alexander roshal extracting testrar.part1.rar extracting testfile.mkv 23% extracting testrar.part2.rar ... testfile.mkv 46% extracting testrar.part3.rar ... testfile.mkv 70% extracting testrar.part4.rar ... testfile.mkv 93% extracting testrar.part5.rar ... testfile.mkv ok ok
you doing 1 of following:
- your
unpack
method runs in event dispatcher thread (edt) - your
unpack
method runs in separate thread
if first case, blocking edt, , stops ui being repainted. if case should call method on separate thread, , read following explanation.
if second case, trying update swing ui on different thread other edt. should handle delicately. there different methods doing this. 1 of simplest being swingutilities.invokelater() method.
i hope helps.
Comments
Post a Comment