Creating a Shell Interface
This project consists of modifying a Java program so that it serves as a shell interface that accepts user commands and then executes each command in a separate process external to the Java virtual machine.
Even the code is given here in Java, however, you could change it and use C++ with corresponding changes.

A shell interface provides th user with a prompt, after which the user enters the next command. The example below illustrates the prompt jsh> and the user’s next command: cat This command displays the file on the terminal using the UNIX cat command.
jsh> cat
Perhaps the easiest technique for implementing a shell interface is to have the program first read what the user enters on the command line (here, cat and then create a separate external process that performs the command. We create the separate process using the ProcessBuilder() object.

The main() method presents the prompt jsh> for the java shell and waits to read input from the user. The program is terminated when the user enters <Control><C>.
This project is organized into three parts: 1) creating the external process and executing the command in that process, 2) modifying the shell to allow changing directories, and 3) adding a history feature.
1) Creating the external process
The first part of this project is to modify the main() method in Figure 3.37 so that an external process is created and executes the command specified by the user. Initially, the command must be parsed into separate parameters and passed to the constructor for the ProcessBuilder object.
A java.util.ArrayList – which implements the java.util.List interface – can be used in this instance, where the first element of the list is cat and the second element is This is an especially useful strategy because the number of arguments passed to UNIX commands may vary (the cat command accepts one argument, the cp command accepts two, and so forth).
If the user enters an invalid command, the start() method in the ProcessBuilder class throws an If this occurs, your program should output an appropriate error message and resume waiting for further commands from the user.
1) Changing directories
The next task is to modify the program so that it changes directories. In UNIX systems, we encounter the concept of the current working directory, which is simply the directory you are currently in. The cd command allows a user to change current directories. Your shell interface must support this command. Subsequent commands relate to this current directory.
When the start() method of a subsequent process is invoked, the new process will use this as the current working directory. For example, if one process with a current working directory of /usr/tom invokes the command cd music, subsequent processes must set their working directors to /usr/tom/music before beginning execution. It is important to note that your program must first make sure the new path being specified is a valid directory. If not, your program should output an appropriate error message.
If the user enters the command cd, change the current working directory to the user’s home directory. The home directory for the current user can be obtained by invoking the static getProperty() method in the System class as follows:
2) Adding a history feature
Many UNIX shells provide a history feature that allows users to see the history of commands they have entered and to rerun a command from that history. The history includes all commands that have been entered by the user since the shell was invoked. For example, if the user entered the history command and saw as output:
0 pwd
1 ls –l
2 cat
The history would list pwd as the first command entered, ls –l as the second command, and so on.
Modify your shell program so that commands are entered into history. (hint, the java.util.ArrayList provides a useful data structure for storing these commands). Your program must allow users to rerun commands from their history.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SimpleShell {

    public static void main(String[] args) throws IOException {

       BufferedReader console = new BufferedReader(new InputStreamReader(;
       List<String> history = new ArrayList<>();

       // Home directory and current working directory.
       String hd = System.getProperty("user.dir");
       String cwd = System.getProperty("user.dir");

       // Break out with <control><C>
       while (true) {
            String commandLine = console.readLine();

            // If the use entered a return, just loop again.
            if (commandLine.equals(""))

            // Add current command to the history.

            // Parse the input to obtain the command and any parameter.
            List<String> commands = Arrays.asList(commandLine.split(" "));

            // If the current command is history, handle it without sending to the ProcessBuilder
            if (commands.get(0).equals...
$48.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Web Development (HTML, XML, PHP, JavaScript, Adobe, Flash, etc.) Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats