The project on page 149-152, (page 127-130 for version 7), including two parts, i) create and execute the command specified by the user, such as jsh> more Prog.java. (type command in windows) or jsh> dir ii) change the directory, such as jsh> cd .. (in windows, you need to add cmd /c as the prefix command. You dont need to do the 3 rd feature adding a history feature from the book.

Creating a Shell Interface Using Java

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.

Overview

Ashellinterfaceprovidestheuserwithaprompt,afterwhichtheuserentersthe next command. The example below illustrates the prompt jsh> and the users next command: cat Prog.java . This command displays the file Prog.java on the terminal using the UNIX cat command.

jsh> cat Prog.java

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 Prog.java ) and then create a separate external process that performs the command.Wecreatetheseparateprocessusingthe ProcessBuilder() object, as illustrated in Figure 3.13. In our example, this separate process is external to the JVM and begins execution when its run() method is invoked.

A Javaprogram that providesthe basic operations of a command-line shell is supplied in Figure 3.37. The main() method presents the prompt jsh> (for j ava sh ell) 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.

import java.io.*;
public class SimpleShell
{
public static void main(String[] args) throws
java.io.IOException {
String commandLine;
BufferedReader console = new BufferedReader
(new InputStreamReader(System.in));
// we break out with < control>< C>
while (true) {
// read what the user entered
System.out.print("jsh>");
commandLine = console.readLine();
// if the user entered a return, just loop again
if (commandLine.equals(""))
continue;
/** The steps are:
(1) parse the input to obtain the command and
any parameters
(2) create a ProcessBuilder object
(3) start the process
(4) obtain the output stream
(5) output the contents returned by the command */
}
}
}

Figure 3.37 Outline of simple shell.

Part 1: Creating an 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. For example, if the user enters the command

jsh> cat Prog.java

the parameters are (1) cat and (2) Prog.java , and these parameters must be passed to the ProcessBuilder constructor. Perhaps the easiest strategy for doing this is to use the constructor with the following signature:

public ProcessBuilder (List< String> command)

A java.util.ArrayList whichimplementsthe java.util.List interface can be used in this instance, where the first element of the list is cat and the second element is Prog.java . 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 java.io.IOException . If this occurs, your program should output an appropriate error message and resume waiting for further commands from the user.

Part 2: Changing Directories

The next task is to modify the program in Figure 3.37 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. For example, if the current directory is /usr/tom and the user enters cd music , the current directory becomes /usr/tom/music .Subsequent commandsrelatetothiscurrentdirectory.Forexample,entering ls willoutput all the files in /usr/tom/music .

The ProcessBuilder class provides the following method for changing the working directory:

public ProcessBuilder directory(File directory)

Whenthe start() methodofasubsequentprocessisinvoked,thenewprocess 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 directories 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 users home directory. The home directory for the current user can be obtained by invoking the static getProperty() method in the System class as follows:

System.getProperty("user.dir");
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.