2

This is the code that attempts to run the system command:

String command = "java -cp 1outinstr;out Main";
Process p      = Runtime.getRuntime().exec("cmd /c " + command);

My problem is that I can't see the command's output.

corazza
  • 31,222
  • 37
  • 115
  • 186

2 Answers2

5

The command is running fine, it's just not connected to the console.

If you want to see the commands's output, you'll have to print it yourself:

String command = "java -cp 1outinstr;out Main";
Process p = Runtime.getRuntime().exec("cmd /c " + command);

BufferedReader stdInput = new BufferedReader(new InputStreamReader(
        p.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(
        p.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
}

// read any errors from the attempted command
System.out
        .println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
    System.out.println(s);
}

code taken from another answer

If you want to learn more, read about standard streams:

In computer programming, standard streams are preconnected input and output channels between a computer program and its environment (typically a text terminal) when it begins execution. The three I/O connections are called standard input (stdin), standard output (stdout) and standard error (stderr). - from Wikipedia

Community
  • 1
  • 1
corazza
  • 31,222
  • 37
  • 115
  • 186
0

try this

 String command="java -cp 1outinstr;out Main"
    Process p = Runtime.getRuntime().exec( // "cmd - for windows only"
                        "cmd /c " + command);
    BufferedReader in = new BufferedReader( new InputStreamReader(p.getInputStream()))
    while(in.readline != null)
    {
    String output = in.readline();
    System.out.println(output);
    }
swapnil7
  • 808
  • 1
  • 9
  • 22