import java.io.PrintStream; /** * */ public class PythonInteractive { public static void main(String[] args) throws Exception { ProcessBuilder builder = new ProcessBuilder("python", "-i"); builder.redirectErrorStream(true); Process p = builder.start(); PrintStream printStream = new PrintStream(p.getOutputStream(), true); new Thread(() -> { int r; try { while (p.isAlive()) { while (p.getInputStream().available() > 0) { r = p.getInputStream().read(); System.out.print(Character.toString((char) r)); } Thread.sleep(10); } } catch (Exception ex) { System.err.println(ex); } }).start(); Thread.sleep(200); String cmd = "print (1+2)"; System.out.println(cmd); printStream.println(cmd); Thread.sleep(200); p.destroy(); } }