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

Popular posts from this blog

4x4 Matrix in Python -

python - String indices must be integers and while issue -

wso2is - WSO2 IS 5.0.0 SP1 After restart there is authentication error -