Modifying A Java Program So That It Serves A Interface

Posted Under: Operating Systems

Ask A Question
Viewed 16
I have attached the instructions. I would need the work to be completed on word doc. I have more files to share but it’s not allowing me to share it. In order to complete the work I have 13 hours.

This order does not have tags, yet.

COSC 4337 Operating Systems Dr. Zhu Project 1 Creating a Shell Interface Due Date: 9/26/21 11:59pm These are the instructions for your first Operating Systems programming project. Recall that the project is done by group of 2 students. You could submit only one copy with two names. Problem: 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. Overview: A shell interface provides the 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, 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. COSC 4337 Operating Systems Dr. Zhu A Java program that provides the basic operations of a command-line shell is supplied in Figure 3.37. 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. For example, if the user enters the command jsh> cat COSC 4337 Operating Systems Dr. Zhu the parameters are (1) cat and (2), 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 – 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. COSC 4337 Operating Systems Dr. Zhu 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 commands relate to this current directory. For example, entering ls will output allt eh 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 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: System.getProperty(“user.dir”); 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: 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 a history. (hint, the java.util.ArrayList provides a useful data structure for storing these commands). You program must allow users to rerun commands from their history. COSC 4337 Operating Systems Dr. Zhu What to hand in? • According to the attached report template, please submit a report includes problem statement, analysis, algorithm design, class prototype (class contract), program Input/Output, and tested results (analysis your result to see if it is correct). • The source program files (name your file “” or “project1.cpp”) • You could either submit through Blackboard or my email box. Each Java program should have the following in the beginning: // Program Name: <The name of your Java program file> // Programmer: <Your name here>, <ID> // Assignment Number: <put project number here, e.g. Project #1> // Purpose: <A short problem description>
Explanations and Answers 0

No answers posted

Post your Answer - free or at a fee

Login to your tutor account to post an answer

Posting a free answer earns you +20 points.


NB: Post a homework question for free and get answers - free or paid homework help.

Get answers to: Modifying A Java Program So That It Serves A Interface or similar questions only at Tutlance.

Related Questions