linux - java Runtime.getRuntime().exec cannot get output for some commands -
i on osx 10.8.5 , trying run terminal commands java.
so code below, can read output of runtime.getruntime().exec commands not others.
you see commands return output fine ls , ifconfig, trying openssl return output either returns nothing or hangs depending on implementation.
i have used 2 different methods of reading output same results both.
what preventing commands returning output?
thecommand = "ls"; //this command works ok , returns output //thecommand = "ifconfig"; //this command works ok , returns output //thecommand = "openssl rand"; //this command not return output //thecommand = "/bin/sh openssl rand"; //this command not return output //thecommand = "/bin/sh -c openssl rand"; //this command hangs try { system.out.println("trying..."); process extproc=runtime.getruntime().exec(thecommand); extproc.waitfor(); //read ouput attempt #1 - works okay ls , ifconfig command //bufferedreader readproc=new bufferedreader(new inputstreamreader(extproc.getinputstream())); //while(readproc.ready()) { // thereadbuffer = readproc.readline(); // system.out.println(thereadbuffer); //} //read output attempt #2 - works okay ls , ifconfig command inputstream theinputstream = extproc.getinputstream(); java.util.scanner thescanner = new java.util.scanner(theinputstream).usedelimiter("\\a"); if (thescanner.hasnext()) { thereadbuffer = thescanner.next(); system.out.println(thereadbuffer); } } catch( ioexception e) { system.out.println(e.getmessage()); } catch(interruptedexception e) { system.out.println(e.getmessage()); }
i trying generate random characters command (which works in terminal):
openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
thanks
okay mark w comment original question realized commands output stderr (use geterrorstream) , stdout (use getinputstream).
so doing allows read either:
bufferedreader readinputproc=new bufferedreader(new inputstreamreader(extproc.getinputstream())); while(readinputproc.ready()) { thereadbuffer = readinputproc.readline(); system.out.println(thereadbuffer); } bufferedreader readerrorproc=new bufferedreader(new inputstreamreader(extproc.geterrorstream())); while(readerrorproc.ready()) { thereadbuffer = readerrorproc.readline(); system.out.println(thereadbuffer); }
Comments
Post a Comment