Answer the question
In order to leave comments, you need to log in
How to read from console window if it doesn't output anything to stdout?
Question on C# WinForms (just in case)
Half a day I've been fiddling with launching a console application program from the GUI, so that I would get the text output line by line.
With the help of Process I start the process, I hang handlers on DataRecieved.
For half a day I could not understand why I did not receive anything in response, now I tried it with the standard ipconfig and O MIRACLE!
I concluded that the program I need outputs text to the console in a non-standard way, like I heard about printf from the author, but I'm not sure.
How to be? I really need to parse what the program will output during operation)
Answer the question
In order to leave comments, you need to log in
Poking around in one interesting application, I came across something that might suit you
, the method executes a console command
public static int runProcess(File workDir, String... command) throws Exception {
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(workDir);
pb.environment().put("JAVA_HOME", System.getProperty("java.home"));
if(!pb.environment().containsKey("MAVEN_OPTS")) {
pb.environment().put("MAVEN_OPTS", "-Xmx1024M");
}
Process ps = pb.start();
(new Thread(new Builder.StreamRedirector(ps.getInputStream(), System.out))).start();
(new Thread(new Builder.StreamRedirector(ps.getErrorStream(), System.err))).start();
int status = ps.waitFor();
if(status != 0) {
throw new RuntimeException("Error running command, return status !=0: " + Arrays.toString(command));
} else {
return status;
}
}
private static class StreamRedirector implements Runnable {
private final InputStream in;
private final PrintStream out;
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(this.in));
try {
String ex;
while((ex = br.readLine()) != null) {
this.out.println(ex);
}
} catch (IOException var3) {
throw Throwables.propagate(var3);
}
}
@ConstructorProperties({"in", "out"})
public StreamRedirector(InputStream in, PrintStream out) {
this.in = in;
this.out = out;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question