簡體   English   中英

從exec運行java -jar

[英]Run java -jar from exec

我嘗試運行: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));
        }
}

它什么也沒給我返回...我想攔截輸出流。 我該怎么辦?

恕我直言,您讀取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發送的輸出似乎未到達ProcessInputStream ...

要解決此問題,可以使用ProcessBuilderinheritIO ...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM