简体   繁体   中英

Runtime.getRuntime.exec(“color 0a”) not working?

I tried that to change the color of the DOS window by doing:

Runtime.getRuntime().exec("color 0a");

But it doesn't work and shows me the current exception:

http://postimg.org/image/9o8xj54tf/

That line is in the main of the program.

The 'color' command is actually not an executable binary and therefore cannot be executed outside of cmd.exe .

If you want to open a MS-DOS window from Java, use this code:

Runtime.getRuntime().exec("cmd.exe /K color 0a");

or save your MS-DOS commands to a .bat file and run them in sequence using:

Runtime.getRuntime().exec("cmd.exe /K your_batch_file.bat");

Actually I found a method that works, which is the following:

new ProcessBuilder("cmd.exe", "/c", "color 0a").inheritIO().start();

It starts a process of cmd.exe with the command

color 0a

And then redirects the output to the console.

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