Get output java complie from cmd -
Get output java complie from cmd -
i have project utilize command prompt complie java file,then print result in console,this mycode.
public static void main(string[] args) { string line; string output = ""; seek { process p = runtime.getruntime().exec("java helloworld"); bufferedreader input = new bufferedreader(new inputstreamreader(p.getinputstream())); while ((line = input.readline()) != null) { output += (line + '\n'); } input.close(); } grab (exception ex) { ex.printstacktrace(); } system.out.print(output); }
but show nothing,although work command,please help me.
as 1 of commenters mentioned might result in quite complex setups running into. in case error happens in java , not see output since error messages written stderr stream instead of stdout.
so there 2 options (1) take code have , seek read process' errorstream.
bufferedreader error = new bufferedreader(new inputstreamreader(p.geterrorstream());
or if not care whether or not process starting writing stderr or stdout can utilize processbuilder , set redirect error stream.
processbuilder pb = new processbuilder("java", "helloworld"); pb.redirecterrorstream(true); // redirects stderr stdout process p = pb.start(); bufferedreader input = new bufferedreader(new inputstreamreader(p.getinputstream())); while ((line = input.readline()) != null) { output += (line + '\n'); } input.close();
for sake of simplicity omitted boilerplate code , exception handling in above. think idea.
java
Comments
Post a Comment