Runtime.exec() in java hangs because it is waiting for input from System.in
<p>I have the following short python program "test.py"</p>

<pre><code>n = int(raw_input())
print n
</code></pre>

<p>I'm executing the above program from following java program "ProcessRunner.java"</p>

<pre><code>import java.util.*;
import java.io.*;

public class ProcessRunner {
    public static void main(String[] args) {
        try {
            Scanner s = new Scanner(Runtime.getRuntime().exec("python test.py").getInputStream()).useDelimiter("\\A");
            System.out.println(s.next());
        }

        catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
</code></pre>

<p>Upon running the command,</p>

<pre><code>java ProcessRunner
</code></pre>

<p>I'm not able to pass a value 'n' in proper format to Python program and also the java run hangs. What is the proper way to handle the situation and pass a value to 'n' dynamically to python program from inside java program?</p>

<p><a href="https://docs.python.org/2/library/functions.html?highlight=raw_input#raw_input" rel="nofollow"><code>raw_input()</code></a>, or <a href="https://docs.python.org/3/library/functions.html?highlight=raw_input#input" rel="nofollow"><code>input()</code></a> in Python 3, will block waiting for new line terminated input on standard input, however, the Java program is not sending it anything.</p>

<p>Try writing to the Python subprocess using the stream returned by <code>getOutputStream()</code>. Here's an example:</p>

<pre><code>import java.util.*;
import java.io.*;

public class ProcessRunner {
    public static void main(String[] args) {
        try {
            Process p = Runtime.getRuntime().exec("python test.py");
            Scanner s = new Scanner(p.getInputStream());
            PrintWriter toChild = new PrintWriter(p.getOutputStream());

            toChild.println("1234");    // write to child's stdin
            toChild.close();            // or you can use toChild.flush()
            System.out.println(s.next());
        }

        catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
</code></pre>

<hr>

<p>An alternative is to pass <code>n</code> as a command line argument. This requires modification of the Python script to expect and process the command line arguments, and to the Java code to send the argument:</p>

<pre><code>import java.util.*;
import java.io.*;

public class ProcessRunner {
    public static void main(String[] args) {
        try {
            int n = 1234;
            Process p = Runtime.getRuntime().exec("python test.py " + n);
            Scanner s = new Scanner(p.getInputStream());
            System.out.println(s.next());
        }

        catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
</code></pre>

<p>And the Python script, <code>test.py</code>:</p>

<pre><code>import sys

if len(sys.argv) &gt; 1:
    print int(sys.argv[1]) 
</code></pre>

java python multithreading process runtime.exec