0

I'm try to run: java -jar /home/user/workspace/maltparser-1.8/maltparser-1.8.jar This is work. And return:

-----------------------------------------------------------------------------
                          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

And now, I'm try to run this .jar from my class:

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));
        }
}

And it return me nothing... I want to intercept output stream. How I can do it?

bjabgcjrpxaf
  • 113
  • 1
  • 1
  • 4

1 Answers1

1

IMHO, the your method of reading the InputStream is a little weird, you probably don't want to wait till the stream is fill before printing something out, also, you ignoring the error stream...

I prefer to use ProcessBuilder because...

  1. You can redirect the error stream to the InputStream, which makes it easier to manage and
  2. You can change the context of the working directory from which the command will be started in...

As an example...

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();
}

Updated

For reasons that I'm unaware of, output sent through log4j's ConsoleAppender doesn't seem to reach the Processs InputStream...

To resolve this, you can use ProcessBuilders 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;
        }
    }

}

This is just my test code, so it might be a little over the top ;)

Which finally dumped out...

-----------------------------------------------------------------------------
                          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
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366