0

I have read this question: Java Programming: call an exe from Java and passing parameters

And this answer is good enough https://stackoverflow.com/a/5604756/2674303

But I additionally want to pass parameters to stdin of external process and read from stdout of this process.

How can I do this?

My efforts:

main method:

public class ProcessBuilderTest {
    public static void main(String[] args) throws IOException, InterruptedException {
        ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\Java\\jdk1.8.0_111\\bin\\java",
                "-cp", //class path key
                "C:\\Users\\redwhite\\IdeaProjects\\HelloMyWorld\\out\\production\\HelloMyWorld" , // path to class file of ExternalProcess class 
                "call_external.ExternalProcess"); // fully qualified name
        Process process = pb.start();
        OutputStream processOutputStream = process.getOutputStream();
        IOUtils.write("1" + System.lineSeparator(), processOutputStream);
        InputStream processInputStream = process.getInputStream();
        System.out.println("--1--");
        System.out.println(process.isAlive()); // outputs true
        String result = IOUtils.toString(processInputStream, "UTF-8"); //<-- hangs here
        System.out.println("--2--");
        process.waitFor();
        System.out.println(result); // expect to see processed[1]
    }
}

ExternalProcess await string from stdin and produce another string to stdout:

package call_external;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * Created by redwhite on 27.03.2017.
 */
public class ExternalProcess {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        System.out.println("processed[" + input + "]");
    }
}

This code hangs.I cannot understand why

Community
  • 1
  • 1
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • @Reimeus maybe... I don't know way to check it. For this case I added System.lineSeparator() to the end of input – gstackoverflow Mar 27 '17 at 16:53
  • your code isnt clear. how are the 2 code snippets related? Post a [mcve] – Reimeus Mar 27 '17 at 16:54
  • @Reimeus It is really minimal, complete and so on.In the first part of code I invoke external process. The code of this process provided in the second code snippet – gstackoverflow Mar 27 '17 at 16:57
  • @Reimeus new ProcessBuilder("C:\\Program Files\\Java\\jdk1.8.0_111\\bin\\java", "-cp", **"project_folder\\target\\classes package.ExternalProcess"**); – gstackoverflow Mar 27 '17 at 16:58

3 Answers3

0

I don't know what the IOUtils class is and what the toString() method is doing. However, a way to read from the InputStream is:

InputStreamReader isr;
BufferedReader br;

String input = "";
String line;

try {

  isr = new InputStreamReader( p.getInputStream );
  br = new BufferedReader( isr );

  while( (line = br.readLine()) != null ) {
    input += line + "\n";
  }

} catch( Exception e ) {
  e.printStackTrace();
}
Don Foumare
  • 444
  • 3
  • 13
  • 1
    https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/IOUtils.html#toString(java.io.InputStream,%20java.lang.String) – gstackoverflow Mar 27 '17 at 19:41
0

If I well understood your problem, you are looking for a popen-equivalent function in Java. Maybe this discussion can help you :)

Java: popen()-like function?

Community
  • 1
  • 1
0

Instead of adding \n in the end of line, stream should be closed thus after replacing

IOUtils.write("1" + System.lineSeparator(), processOutputStream);

with

IOUtils.write("1", processOutputStream);
processOutputStream.close();

Code became work properly

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710