I am trying (and failing) to decide how I can run a fully interactive shell program from Java.
Here's the script:
I have a great GUI application that is cross-platform and written in Java. In that I am trying to add an interactive command line environment to run headless. This side of things is beautiful and dandy. However, one of the functions of the main GUI is file editing. Now for the command line interface, I want to be able to run an external editor to edit files, and then go back to where I was after I saved and exited. For example, on Linux, it can execute "vi / path / to / file".
So, how can I execute this command in such a way that the keyboard and display are fully interactive for the application? I do not want it to work in the background. I don’t want it to redirect IO through Java, I just want one single command to run “in the foreground” as long as it does not exist.
Just as if I were using a function system()in C.
Everything that I have found so far executes commands in the background or passes IO through Java, which will not work for interactive (non-linear) applications.
Oh, and one final caveat: I'm limited to compatibility with Java 1.6, so I can't do fancy things with ProcessBuilder.
Here's the required SSCCE:
class exetest {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("vi");
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
, Runtime.getRuntime(). exec() , vi, (RAW, ) vi , , .
C:
void main() {
system("vi");
}
Update:
, a) Linux/OSX ( , -), b) :
import java.io.*;
class exetest {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("/bin/bash");
OutputStream stdin = p.getOutputStream();
PrintWriter pw = new PrintWriter(stdin);
pw.println("vi < /dev/tty > /dev/tty");
pw.close();
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}