I'm making a terminal app and using the following code
try {
process = runtime.exec(command);
BufferedReader stream = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = stream.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append('\n');
}
stream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((line = stream.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append('\n');
}
} catch (IOException e) {
e.printStackTrace();
}
Problem is that everytime exec()
is executed, a new shell
is created. (That's what I read)
Let's say I execute cd
, it will change the directory, but next time I execute a command, it will execute from /
.
How do I use the same shell till the app is closed?
PS: This question is not same as How to execute cmd commands via Java I am able to execute commands, no problem! I'm not trying to execute known commands by putting them in source code. I'm getting the commands from the user. Problem is I'm not able to maintain the shell environment constant.