0

I'm making a text editor in Java, and I want it to be able to run a Python file in the OS's command line. So on windows, I want the Python file to be run in cmd, and in Mac, Terminal, and so on.. I can already determine the operating system of the user, this isn't a problem. The problem is that I can't get it to run in the command line. Even if I try to make it only windows compatible and always run in cmd. I use this code to run the file, which I found from another question on stackoverflow. (I have been trying to find an answer for a while now)

ProcessBuilder builder = new ProcessBuilder(
    "cmd.exe", "/c", cmd2Run.getText());
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
    line = r.readLine();
    if (line == null) {
        break;
    }
    System.out.println(line);
}

But, as I was expecting (because of the System.out.println...) it does run the Python file, but outputs it to the Java console. This would be fine, but then I thought "what if the user wants input?". So I realised I need to run the file in cmd. Is this possible? I wouldn't have thought it would be so difficult.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Jacob Garby
  • 773
  • 7
  • 22
  • 6
    Identical to this? http://stackoverflow.com/questions/4688123/how-to-open-the-command-prompt-and-insert-commands-using-java – Koen De Groote Sep 05 '16 at 12:42
  • @KoenDeGroote I'll have a look at this, but I think I've tried something similar already – Jacob Garby Sep 05 '16 at 13:06
  • If you don't want to print to the console, just remove the `System.out.println` statement. Append to a `StringBuilder` instead. – Robert Sep 05 '16 at 13:09
  • @KoenDeGroote well.. This did actually work, it just took a bit of modifying as it wasn't explained well – Jacob Garby Sep 05 '16 at 13:13

0 Answers0