简体   繁体   中英

How to use Runtime.getRuntime().exec(command) in windows?

How to use Runtime.getRuntime().exec(command) in windows?

1.
command = "cat data.json"; // works in linux terminal
Runtime.getRuntime().exec(command) // runs in linux => Runs OK

2.
command = "type data.json"; // works in windows cmd
Runtime.getRuntime().exec(command) // runs in windows => Fails to run

3.
command = "cmd /C type data.json"; // works in windows cmd
Runtime.getRuntime().exec(command) // runs in windows => Runs OK
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); // Output is empty

In the third scenario, the output from input.readLine() is empty, but if I use command "type data.json" in cmd, it'll print the json the same way it would on linux with "cat data.json". I'm confused to what is going on? I'd like to run the COMMAND on both windows and linux. Any help is appreciated!

Thanks!

Try redirecting the error stream on the process by using a ProcessBuilder . In all likelyhood the command is writing out to STDErr. You could also get that stream by using Process.getErrorStream().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM