When executing an external process with Runtime.exec() its standard input and output streams do not connect to the terminal from which you run the Java program. You can use shell redirection to connect it, but first you need to know which terminal to use. It is not possible to find a terminal using the standard API, but you can probably find an open source library that does this.
To make sure this can be done, this program opens in less :
public class Test { public static void main(String[] args) throws Exception { Process p = Runtime.getRuntime().exec( new String[] {"sh", "-c", "less Test.java < "+args[0] + " > "+args[0]}); System.out.println("=> "+p.waitFor()); } }
To run it, you must use java Test $(tty) . The tty program prints the name of the terminal connected to its stdin.
I am not too sure about the portability of this solution; at least it works on Linux.
source share