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

193 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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.

1
On

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");