Capturing stdout when calling Runtime.exec

后端 未结 8 1194
终归单人心
终归单人心 2020-11-21 23:26

When experiencing networking problems on client machines, I\'d like to be able to run a few command lines and email the results of them to myself.

I\'ve found Runtim

8条回答
  •  伪装坚强ぢ
    2020-11-21 23:37

    Use Plexus Utils, it is used by Maven to execut all external processes.

    Commandline commandLine = new Commandline();
    commandLine.setExecutable(executable.getAbsolutePath());
    
    Collection args = getArguments();
    
    for (String arg : args) {
        Arg _arg = commandLine.createArg();
        _arg.setValue(arg);
    }
    
    WriterStreamConsumer systemOut = new WriterStreamConsumer(console);
    WriterStreamConsumer systemErr = new WriterStreamConsumer(console);
    
    returnCode = CommandLineUtils.executeCommandLine(commandLine, systemOut, systemErr, 10);
    if (returnCode != 0) {
        // bad
    } else {
        // good
    }
    

提交回复
热议问题