clankill3r
clankill3r

Reputation: 9543

Java Program that exec commands won't continue

I try to get my penplotter to work from within java. I have a start but I don't know how to continue. This is what I have:

public static void main(String[] args) {

        try {
            Runtime runTime = Runtime.getRuntime();

            Process process = runTime.exec("chiplotle");


            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line = null;

            System.out.println("this prints fine");


            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }


            System.out.println("it never reaches this...");

        }
        catch (IOException e) {
            e.printStackTrace();
        }

    }

This is the output in the console:

IntelliJ console

I typed the 11 myself. But it doesn't do anything with it. Also it never prints:

System.out.println("it never reaches this...");

So it looks like my program is halted for input, is that correct? And how can I get further?

Upvotes: 1

Views: 53

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

  1. You should read from the InputStream in a bacgkround thread.
  2. You need to get the Process's OutputStream and then write to it.

OutputStream os = process.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
PrintWriter pw = new PrintWriter(bw);

// now you can write to the Process, i.e., pw.println("11");

You will need to not just print but also to analyze the text that your InputStream sends you to decide when to write back to the process via the PrintWriter.

Upvotes: 1

Related Questions