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