从exec运行java -jar

时间:2014-10-30 03:42:44

标签: java

我试着跑: java -jar /home/user/workspace/maltparser-1.8/maltparser-1.8.jar 这是工作。并返回:

-----------------------------------------------------------------------------
                          MaltParser 1.8                             
-----------------------------------------------------------------------------
         MALT (Models and Algorithms for Language Technology) Group          
             Vaxjo University and Uppsala University                         
                             Sweden                                          
-----------------------------------------------------------------------------

Usage:     java -jar maltparser-1.8.jar -f <path to option file> <options>    java -jar maltparser-1.8.jar -h for more help and options

help                  (  -h) : Show options                           

----------------------------------------------------------------------------- option_file           (  -f) : Path to option file                    

----------------------------------------------------------------------------- verbosity            *(  -v) : Verbosity level                         debug      - Logging of debugging messages   error      - Logging of error events   fatal      - Logging of very severe error events   info
- Logging of informational messages   off        - Logging turned off     warn       - Logging of harmful situations
-----------------------------------------------------------------------------

Documentation: docs/index.html

现在,我尝试从我的班级运行这个.jar:

public class Main {

        public static void main(String[] args) throws IOException, InterruptedException {

                    Process ps = Runtime.getRuntime().exec(new String[]{"java","-jar","/home/user/workspace/maltparser-1.8/maltparser-1.8.jar"});
                    ps.waitFor();
                    java.io.InputStream is=ps.getInputStream();
                    byte b[]=new byte[is.available()];
                    is.read(b,0,b.length);
                    System.out.println(new String(b));
        }
}

它没有回复我...我想拦截输出流。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

恕我直言,你阅读InputStream的方法有点奇怪,你可能不想等到流被填充之后才打印出来的东西,同样,你忽略错误流... < / p>

我更喜欢使用ProcessBuilder,因为......

  1. 您可以将错误流重定向到InputStream,这样便于管理和
  2. 您可以在...
  3. 中更改将从中启动命令的工作目录的上下文

    作为一个例子......

    try {
        ProcessBuilder pb = new ProcessBuilder(new String[]{"java", "-jar", "Your.jar"});
        pb.redirectError();
        //pb.directory(new File("you/path"));
        Process ps = pb.start();
        try (java.io.InputStream is = ps.getInputStream()) {
            int read = -1;
            while ((read = is.read()) != -1) {
                System.out.print((char) read);
            }
        }
        System.out.println("Command exited with: " + ps.waitFor());
    } catch (IOException | InterruptedException exp) {
        exp.printStackTrace();
    }
    

    <强>更新

    由于我不知道的原因,通过log4j的ConsoleAppender发送的输出似乎无法到达Process s InputStream ...

    要解决此问题,您可以使用ProcessBuilder s inheritIO ...

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class TestRunJar {
    
        public static void main(String[] args) {
            try {
                ProcessBuilder pb = new ProcessBuilder(new String[]{"java", "-jar", "/../maltparser-1.8.jar"});
                pb.inheritIO();
                pb.redirectError();
                pb.directory(new File("/..")); // Path to where maltparser-1.8.jar resides
                Process ps = pb.start();
    
                InputStreamConsumer stdout = new InputStreamConsumer(ps.getInputStream());
                InputStreamConsumer stderr = new InputStreamConsumer(ps.getErrorStream());
    
                stderr.start();
                stdout.start();
    
                stderr.join();
                stderr.join();
    
                System.out.println("Command exited with: " + ps.waitFor());
            } catch (IOException | InterruptedException exp) {
                exp.printStackTrace();
            }
        }
    
        public static class InputStreamConsumer extends Thread {
    
            private InputStream is;
            private IOException exp;
            private StringBuilder output;
    
            public InputStreamConsumer(InputStream is) {
                this.is = is;
            }
    
            @Override
            public void run() {
                int in = -1;
                output = new StringBuilder(64);
                try {
                    while ((in = is.read()) != -1) {
                        output.append((char) in);
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                    exp = ex;
                }
            }
    
            public StringBuilder getOutput() {
                return output;
            }
    
            public IOException getException() {
                return exp;
            }
        }
    
    }
    

    这只是我的测试代码,所以它可能有点超过顶部;)

    最终抛弃了......

    -----------------------------------------------------------------------------
                              MaltParser 1.8                             
    -----------------------------------------------------------------------------
             MALT (Models and Algorithms for Language Technology) Group          
                 Vaxjo University and Uppsala University                         
                                 Sweden                                          
    -----------------------------------------------------------------------------
    
    Usage: 
       java -jar maltparser-1.8.jar -f <path to option file> <options>
       java -jar maltparser-1.8.jar -h for more help and options
    
    help                  (  -h) : Show options                                 
    -----------------------------------------------------------------------------
    option_file           (  -f) : Path to option file                          
    -----------------------------------------------------------------------------
    verbosity            *(  -v) : Verbosity level                              
      debug      - Logging of debugging messages
      error      - Logging of error events
      fatal      - Logging of very severe error events
      info       - Logging of informational messages
      off        - Logging turned off  
      warn       - Logging of harmful situations
    -----------------------------------------------------------------------------
    
    Documentation: docs/index.html
    Command exited with: 0