Java, Python and IO inheritance

If you remember old way of reading/writing streams between JVM and external process, you probably remember how tedious it was in the past (all these Gobblers, Threads, etc.). Today, it’s way more simple. All you have to do, is inheriting JVM’s IO.

Let’s say you want to run simple Python code that reads something from input stream.

value = raw_input("Type the number? ")
print "Your number is: " + value

all you have to do is to call it from ProcessBuilder and inherit the IO.

public class PythonProcess {
  public static void main(String [] arg) throws Exception {
    ProcessBuilder pb =
      new ProcessBuilder("python", "script.py").inheritIO();
    Process p = pb.start();
    p.waitFor();
  }
}

After this is done you can simply run the code

> javac PythonProcess.java
> java -cp . PythonProcess
Type the number? 44
Your number is: 44