не могу прочитать системный вывод подпроцесса, который я запустил с помощью runtime.exec - PullRequest
0 голосов
/ 16 октября 2018

Я пытаюсь создать подпроцесс, отправить ему два целых числа, а затем получить сумму этих целых чисел следующим образом:

это основной класс процесса:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class ParentClass {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            Process p = Runtime.getRuntime().exec("java -cp C:\\Users\\LENOVO\\eclipse-workspace\\TrheadComunication\\bin ChildClass");
            DataOutputStream out = new DataOutputStream(p.getOutputStream());
            DataInputStream in = new DataInputStream(p.getInputStream());
            in.read();
            int a = scanner.nextInt();
            out.writeInt(a);
            out.flush();
            int b = scanner.nextInt();
            out.writeInt(b);
            out.flush();
            int resultat = 20;
            while (resultat == 20)
                resultat = in.read();
            System.out.println(a + " + " + resultat);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

и это класс подпроцесса:

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class ChildClass{

    public static void main(String[] args) throws InterruptedException {
        Scanner scanner = new Scanner(System.in);
        int a = 0;
        while (a == 0)
            a = scanner.nextInt();
        int b = 0;
        while (b == 0)
            b = scanner.nextInt();
        TimeUnit.SECONDS.sleep(1);
        System.out.print(a + b);
        System.out.flush();
    }

}

обратите внимание, что я добавил циклы и инструкцию сна как неудачную попытку заставить их работать вместе.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...