2

I'm trying to start a process using Java either using ProcessBuilder or apache DefaultExecutor as another user and be able to read stdout of that process: sample code that I have:

OutputStream out = null;
OutputStream err = null;
InputStream in = null;
String line = "runas /noprofile /user:dev /savecred /env \"java -cp myProj.jar com.myComp.myProj.Main -libjardirs target/lib\"";
CommandLine cmdLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(0);

PumpStreamHandler streamHandler = new PumpStreamHandler(out, err, in);
executor.setStreamHandler(streamHandler);

streamHandler.start();
ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);

executor.setWatchdog(watchdog);
int exitValue = executor.execute(cmdLine);

I tried different variations but so far no luck. Runas starts another process that executes, but stdout only gets info from runas itself which returns nothing and the main thing it returns before that process finishes. I don't want to continue to execute my program before execution of that jar finishes, and I also want to be able to get stdout of that jar while it executes (or at least when it finishes to execute). Also need to be able to terminate execution of that jar if it reaches timelimt

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • Try processBuilder.redirectOutput(Redirect.INHERIT) http://stackoverflow.com/questions/10540631/how-to-redirect-child-process-stdout-stderr-to-the-main-process-stdout-stderr-in – MGorgon Aug 02 '13 at 21:31
  • @MGorgon that won't help as an answer. The problem is that the output that the OP wants is not occurring in the sub-process being created but instead in yet another detached process which is launched by `RunAs`. – Tim Bender Aug 02 '13 at 21:46
  • That won't help since we're using Java 6 and `processBuilder.redirectOutput(Redirect.INHERIT)` only exists in Java 7 and I'm not allowed to change project to newest Java – user2647364 Aug 05 '13 at 14:35

2 Answers2

2

From what I've read, Microsoft's runas command launches a new terminal when invoked. There is no output piping from the new terminal back to the terminal which invoked it. The closest solution suggested is to include a pipe as part of the command for runas to invoke so that the output of the command run in the new terminal is written to a file. Following this notion, you could also have a flag/signal file written as the last thing before the runas terminal exits.

This will be done in one line:

runas /noprofile /user:dev /savecred /env "cmd.exe /c java -cp myProj.jar com.myComp.myProj.Main -libjardirs target/lib 2> cmd.log & echo about to close terminal>>cmd.log"

Last portion & echo about to close terminal>>cmd.log uses & to issue a second command in the RunAs terminal. Text from echo is appended to the end of the same log file: cmd.log using >> for append. The program reading the output file will then have to look for that text as the poison pill indicator that the sub-process has completed.

Community
  • 1
  • 1
Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • How do you add flag/signal written before `runas` terminal exits? – user2647364 Aug 05 '13 at 14:32
  • Thanks Tim, actually got to that answer myself, while was waiting on your answer. For those that will need it later you can do it all in one line: `runas /noprofile /user:dev /savecred /env "cmd.exe /c java -cp myProj.jar com.myComp.myProj.Main -libjardirs target/lib 2> cmd.log & echo about to close terminal>>cmd.log"` Last portion `& echo about to close terminal>>cmd.log` makes adding text from `echo` in the same command line using `&` and appending it to the end of the same log file: `cmd.log` using `>>` for append – user2647364 Aug 05 '13 at 16:50
  • Also using `2>` copies both stdout and stderr to a file, in my case to cmd.log – user2647364 Aug 05 '13 at 16:58
  • I added your comments to "complete" the answer. Sorry for not supplying the complete shell command initially. – Tim Bender Aug 05 '13 at 17:46
0

the windows build-in runas program didn't support pipe so I write a new runas project and use pipe to redirect stdout.May that help you.

傅继晗
  • 927
  • 1
  • 8
  • 14