You need to read InputStream from the process, here is an example:
Edit I modified the code as suggested here to receive the errStream with the stdInput
ProcessBuilder builder = new ProcessBuilder("command goes here");
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream is = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
For debugging purpose, you can read the input as bytes instead of using readLine just in case that the process does not terminate messages with newLine
Here are the primary differences between using System.out/.err/.in and System.console():
System.console()returns null if your application is not run in a terminal (though you can handle this in your application)System.console()provides methods for reading password without echoing charactersSystem.outandSystem.erruse the default platform encoding, while theConsoleclass output methods use the console encoding
This latter behaviour may not be immediately obvious, but code like this can demonstrate the difference:
public class ConsoleDemo {
public static void main(String[] args) {
String[] data = { "\u250C\u2500\u2500\u2500\u2500\u2500\u2510",
"\u2502Hello\u2502",
"\u2514\u2500\u2500\u2500\u2500\u2500\u2518" };
for (String s : data) {
System.out.println(s);
}
for (String s : data) {
System.console().writer().println(s);
}
}
}
On my Windows XP which has a system encoding of windows-1252 and a default console encoding of IBM850, this code will write:
???????
?Hello?
???????
โโโโโโโ
โHelloโ
โโโโโโโ
Note that this behaviour depends on the console encoding being set to a different encoding to the system encoding. This is the default behaviour on Windows for a bunch of historical reasons.
They're essentially the same, if your program is run from an interactive prompt and you haven't redirected stdin or stdout:
public class ConsoleTest {
public static void main(String[] args) {
System.out.println("Console is: " + System.console());
}
}
results in:
$ java ConsoleTest
Console is: java.io.Console@2747ee05
$ java ConsoleTest </dev/null
Console is: null
$ java ConsoleTest | cat
Console is: null
The reason Console exists is to provide features that are useful in the specific case that you're being run from an interactive command line:
- secure password entry (hard to do cross-platform)
- synchronisation (multiple threads can prompt for input and
Consolewill queue them up nicely, whereas if you used System.in/out then all of the prompts would appear simultaneously).
Notice above that redirecting even one of the streams results in System.console() returning null; another irritation is that there's often no Console object available when spawned from another program such as Eclipse or Maven.
Videos
To be honest I find it odd in a GUI environment to accept input from the user via command line instead of some dialog (I mean you display a GUI) but you could just use a Runtime to execute a command prompt. Look here cmd using java
I imagine this requirement is coming from you using a simple logging strategy in your app by just using System.println() statements. You should wrap you System.println() method calls in some kind of logger pattern class. You can create a Logger interface with a Log method. The ConsoleLogger impl would just write to the console and would be useful for you in testing/debugging.
Then for your real users, you have a couple options. You could use a FileLogger and make them read log files if they want to see output of your app. Or you could make some kind of CustomTextWindowLogger that logs to some GUI window, or maybe still a file that a GUI window could display data from.
Either way, I have never seen an app redirect System.println() statements to a graphical component because it it just sends data to the standard output stream. Along those line of thinking, I guess you could also trying changing the System.out output stream to a different stream which your GUI window can display. This would be easier to implement quickly but would be harder to change down the line. With the Logger interface, you can change your mind easily about how the output is given to the user.