runtime - read the output from java exec -
hello have question java. here code:
public static void main(string[] args) throws exception { process pr = runtime.getruntime().exec("java -version"); bufferedreader in = new bufferedreader(new inputstreamreader(pr.getinputstream())); string line; while ((line = in.readline()) != null) { system.out.println(line); } pr.waitfor(); system.out.println("ok!"); in.close(); system.exit(0); }
in code i'am trying java version command execute ok, can't read output return null. why?
use geterrorstream().
bufferedreader in = new bufferedreader(new inputstreamreader(pr.geterrorstream()));
edit:
you can use processbuilder (and read documentation)
processbuilder ps=new processbuilder("java.exe","-version"); //from doc: initially, property false, meaning //standard output , error output of subprocess sent 2 //separate streams ps.redirecterrorstream(true); process pr = ps.start(); bufferedreader in = new bufferedreader(new inputstreamreader(pr.getinputstream())); string line; while ((line = in.readline()) != null) { system.out.println(line); } pr.waitfor(); system.out.println("ok!"); in.close(); system.exit(0);
Comments
Post a Comment