Making a system call that returns the stdout output as a string

前端 未结 27 1210
误落风尘
误落风尘 2020-11-27 05:13

Perl and PHP do this with backticks. For example,

$output = `ls`;

Returns a directory listing. A similar function, system(\"foo\")

27条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 05:49

    Years ago I wrote a plugin for jEdit that interfaced to a native application. This is what I used to get the streams off the running executable. Only thing left to do is while((String s = stdout.readLine())!=null){...}:

    /* File:    IOControl.java
     *
     * created: 10 July 2003
     * author:  dsm
     */
    package org.jpop.io;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    
    /**
     *  Controls the I/O for a process. When using the std[in|out|err] streams, they must all be put on
     *  different threads to avoid blocking!
     *
     * @author     dsm
     * @version    1.5
     */
    public class IOControl extends Object {
        private Process process;
        private BufferedReader stdout;
        private BufferedReader stderr;
        private PrintStream stdin;
    
        /**
         *  Constructor for the IOControl object
         *
         * @param  process  The process to control I/O for
         */
        public IOControl(Process process) {
            this.process = process;
            this.stdin = new PrintStream(process.getOutputStream());
            this.stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
            this.stderr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        }
    
        /**
         *  Gets the stdin attribute of the IOControl object
         *
         * @return    The stdin value
         */
        public PrintStream getStdin() {
            return this.stdin;
        }
    
        /**
         *  Gets the stdout attribute of the IOControl object
         *
         * @return    The stdout value
         */
        public BufferedReader getStdout() {
            return this.stdout;
        }
    
        /**
         *  Gets the stderr attribute of the IOControl object
         *
         * @return    The stderr value
         */
        public BufferedReader getStderr() {
            return this.stderr;
        }
    
        /**
         *  Gets the process attribute of the IOControl object. To monitor the process (as opposed to
         *  just letting it run by itself) its necessary to create a thread like this: 
         *. IOControl ioc;
         *.
         *. new Thread(){
         *.     public void run(){
         *.         while(true){    // only necessary if you want the process to respawn
         *.             try{
         *.                 ioc = new IOControl(Runtime.getRuntime().exec("procname"));
         *.                 // add some code to handle the IO streams
         *.                 ioc.getProcess().waitFor();
         *.             }catch(InterruptedException ie){
         *.                 // deal with exception
         *.             }catch(IOException ioe){
         *.                 // deal with exception
         *.             }
         *.
         *.             // a break condition can be included here to terminate the loop
         *.         }               // only necessary if you want the process to respawn
         *.     }
         *. }.start();
         *  
    * * @return The process value */ public Process getProcess() { return this.process; } }

提交回复
热议问题