If you execute commands with exec() , you are not writing a terminal emulator; you are writing a shell. In this case, you will need to keep track of things that the shell is tracking, such as environment variables and the working directory.
If you really want to write a terminal emulator, you will talk to the shell process through a pseudo-terminal. Then your program will simply track what the terminal is monitoring, for example, the status of the line and what appears on the screen.
Working with a pseudo-terminal from Java will be a bit complicated, because most of the documentation assumes that you are using a C api. man pty should start. Your Java process will need to open the main side of the pseudo-terminal using FileStream objects. I am not sure that in Java there is a way to get a child process to open the slave side of the pseudo-terminal; you may need to invoke a shell command using exec() , which will run another shell command with standard input / output / error redirected to the slave side of the pseudo-terminal.
source share