One way would be to use a Java™
Runtime
class to create an native console instance. This would allow you to retrieve all the information that would normally be output in the console.
The following example shows how to get a
Runtime
object, get the
Process
resulting from executing a command, and how to manage the streams for the process, as well as the streams for the file(s).
The example is an example for displaying the directory entries (Microsoft™ Windows®).
package codeproject.console;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class SampleProgram {
public static boolean isAlive(Process process) {
try {
process.exitValue();
return false;
}
catch (IllegalThreadStateException e) {
return true;
}
}
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
Process process;
int value;
try {
process = runtime.exec("cmd");
}
catch (IOException exception) {
exception.printStackTrace();
return;
}
try (FileOutputStream fstdc = new FileOutputStream("output.log");
FileOutputStream ferrc = new FileOutputStream("error.log");
OutputStream outc = process.getOutputStream();
InputStream inc = process.getInputStream();
InputStream errc = process.getErrorStream()) {
outc.write("dir\r\n".getBytes());
outc.flush();
outc.write("exit\r\n".getBytes());
outc.flush();
while (isAlive(process) == true) {
if (inc.available() == 0 && errc.available() == 0) {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
if (inc.available() > 0) {
while (inc.available() > 0) {
value = inc.read();
System.out.print((char) value);
fstdc.write((char) value);
}
}
if (errc.available() > 0) {
while (errc.available() > 0) {
value = errc.read();
System.err.print((char) value);
ferrc.write((char) value);
}
}
}
}
catch (IOException exception) {
exception.printStackTrace();
}
}
}
For simple time-stamp logging, with basic user input support:
package codeproject.console;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
public class SampleTimeLogProgram {
public static String timeStamp() {
return String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM", Calendar.getInstance());
}
public static boolean isAlive(Process process) {
try {
process.exitValue();
return false;
}
catch (IllegalThreadStateException e) {
return true;
}
}
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
Process process;
InputStream userInput = System.in;
int value;
try {
process = runtime.exec("cmd");
}
catch (IOException exception) {
exception.printStackTrace();
return;
}
try (FileOutputStream fstdc = new FileOutputStream("output.log", true);
FileOutputStream ferrc = new FileOutputStream("error.log", true);
OutputStream outc = process.getOutputStream();
InputStream inc = process.getInputStream();
InputStream errc = process.getErrorStream()) {
String stamp = timeStamp();
fstdc.write(("\r\n" + stamp + " ").getBytes());
ferrc.write(("\r\n" + stamp + " ").getBytes());
System.out.print(stamp + " ");
while (isAlive(process) == true) {
if (inc.available() == 0 && errc.available() == 0) {
if (userInput != null && (userInput.available() > 0)) {
value = userInput.read();
outc.write((char) value);
outc.flush();
}
else {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (inc.available() > 0) {
while (inc.available() > 0) {
value = inc.read();
fstdc.write((char) value);
System.out.print((char) value);
if (value == '\n') {
stamp = timeStamp();
fstdc.write((stamp + " ").getBytes());
System.out.print(stamp + " ");
}
System.out.flush();
}
}
if (errc.available() > 0) {
while (errc.available() > 0) {
value = errc.read();
ferrc.write((char) value);
System.err.print((char) value);
if (value == '\n') {
stamp = timeStamp();
ferrc.write((stamp + " ").getBytes());
System.err.print(stamp + " ");
}
System.err.flush();
}
}
}
}
catch (IOException exception) {
exception.printStackTrace();
}
}
}