How to retain the shell returned from runtime.exec(); <p>I'm making a terminal app and using the following code</p> <pre><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(); } </code></pre> <p>Problem is that everytime <code>exec()</code> is executed, a new <code>shell</code> is created. (That's what I read)</p> <p>Let's say I execute <code>cd</code>, it will change the directory, but next time I execute a command, it will execute from <code>/</code>.</p> <p>How do I use the same shell till the app is closed?</p> <p><strong>PS:</strong> This question is not same as <a href="http://stackoverflow.com/questions/4157303/how-to-execute-cmd-commands-via-java">How to execute cmd commands via Java</a> 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. </p>