0
import java.io.*;

public class ColorTest {
    public static void main(String [] args){
        try{
            //Process p = Runtime.getRuntime().exec("cmd /c color 0a");
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = in.readline()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I am not understanding why the command won't execute. The line that is commented is the one I need help with.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • Read the javadocs for getRuntime() – FredK Aug 05 '15 at 16:33
  • java is a case sensitive language – Madhawa Priyashantha Aug 05 '15 at 16:35
  • It looks like you're expecting the launched `cmd /c` command to affect the command window, if any, in which the application is run. What happens if you use a different command that provides output? What happens if you `cmd /k`? – Andy Thomas Aug 05 '15 at 16:43
  • @AndyThomas cmd /k still doesn't run color but runs other commands like help but gets stuck in infinite loop I have to ctrl c out of it. Cmd /c allows you to enter another command after application is done – user4508513 Aug 05 '15 at 16:49
  • It must be something wrong with your command (cmd). If you replace `exec("cmd ...)` with `exec("ping google.com")` it works just fine. – carpenter Aug 05 '15 at 17:29
  • Seems like commands like title and color don't work – user4508513 Aug 05 '15 at 17:39
  • I suspect the cmd.exe process you're launching from Java doesn't have a console to affect. You could give it a console to affect by using `start` to create a new window. For a trivial example - `start cmd /k color 0a`. – Andy Thomas Aug 05 '15 at 17:58
  • Nope throws IOException – user4508513 Aug 05 '15 at 18:03
  • @AndyThomas throws IOException – user4508513 Aug 05 '15 at 18:03
  • Sorry, from Java that would be `cmd /k start color 0a`. – Andy Thomas Aug 05 '15 at 18:09
  • @AndyThomas thanks got it but how would I apply it to my current window – user4508513 Aug 05 '15 at 18:18
  • Don't know if you can with the `color` command called from Java. See also the answers for Windows environments for this similar question - http://stackoverflow.com/questions/1448858/how-to-color-system-out-println-output/4333226 . Note that some of the answers are not for the Windows command console. – Andy Thomas Aug 05 '15 at 19:36
  • See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. Also, break a `String command` into `String[] commands` in order to preserve spaces in paths etc. – Andrew Thompson Aug 06 '15 at 11:26

0 Answers0