Question: import java.io.*; import java.util.*; public class test{ public static String separator = ; public static void main(String [] args) throws java.io.IOException { String commandLine; BufferedReader
import java.io.*; import java.util.*;
public class test{
public static String separator = "\\";
public static void main(String [] args) throws java.io.IOException {
String commandLine; BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); ProcessBuilder pb = new ProcessBuilder();
while(true) { // user input System.out.print("jsh>"); commandLine = console.readLine();
// If they entered a return, just loop again if(commandLine.equals("")) { continue; }
// Now parse the input to obtain the commands and params // Create a StringTokenizer object and ArrayList object StringTokenizer st = new StringTokenizer(commandLine); ArrayList
// Store commands while (st.hasMoreTokens()){ myCommands.add(st.nextToken()); } if (myCommands.contains("cd")){ if (myCommands.get(myCommands.size()-1).equals("cd")){ File home = new File(System.getProperty("user.home")); System.out.println("your home directory is " + home); pb.directory(home); continue; }else{ String argument = myCommands.get(myCommands.size()-1); System.out.println("argument passed is " + argument); String createPath = pb.directory() + separator + argument; System.out.println("path created is " + createPath); File newPath = new File(createPath); pb.directory(newPath); continue; }//else }//check
// Create the Process builder object pb.command(myCommands);
// Now start the process Process process = pb.start();
// Obtain the output stream InputStream is = process.getInputStream(); // Read what the process returned InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null){ // Content returned by the command System.out.println(line); } br.close(); } } }
//// this is my code .i need part 1 , 2 and 3.
Part 1: Creating an External Process
The first part is to modify the main() method in Figure 1 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
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 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 1 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 commands relate to this current directory. For example, entering ls will output all the files in /usr/tom/music.
The ProcessBuilder class provides the following method for changing the working directory:
public ProcessBuilder directory(File 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 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");
Part 3: 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:
pwd
ls -l
cat Prog.java
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 a 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 by supporting the following three techniques:
When the user enters the command history, you will print out the contents of the history of commands that have been entered into the shell, along with the command numbers.
When the user enters !!, run the previous command in the history. If there is no previous command, output an appropriate error message.
When the user enters !
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
