2014-10-30 121 views
0

我試着運行: java -jar /home/user/workspace/maltparser-1.8/maltparser-1.8.jar 這是工作。與迴歸:從exec運行java -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)); 
     } 
} 

,並返回我什麼......我想攔截輸出流。我如何做到這一點?

+0

嘗試把'waitFor'後,您已經嘗試閱讀'InputStream',因爲[示例](http://stackoverflow.com/questions/15218892/running-a-java-program-from-another-java-program/15220419#15220419) – MadProgrammer 2014-10-30 03:50:35

+0

它不工作太。 – bjabgcjrpxaf 2014-10-30 03:58:40

+0

沒有冒犯,但你閱讀流的方法(恕我直言)是有點奇怪... – MadProgrammer 2014-10-30 04:00:45

回答

1

恕我直言,你的讀取InputStream方法是有點怪異,你可能不希望等到流是打印出來的東西,另外,你忽略錯誤流之前填寫...

我更喜歡使用ProcessBuilder因爲......

  1. 您可以重定向錯誤流到InputStream,這使得它更易於管理和
  2. 您可以更改工作目錄的情況下,從該命令會開始於...

舉個例子...

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小號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 
+0

它只是返回:命令退出:0 – bjabgcjrpxaf 2014-10-30 04:17:34

+0

命令已成功執行... – MadProgrammer 2014-10-30 04:18:21

+0

是的。但是,如果我在終端中運行這個jar,我會看到輸出消息。 – bjabgcjrpxaf 2014-10-30 04:19:19