Question: import java.io . * ; import java.util. * ; public class TestProcessBuilder { static void createProcess ( String command ) throws java.io . IOException {

import java.io.*;
import java.util.*;
public class TestProcessBuilder {
static void createProcess(String command) throws java.io.IOException {
List input = Arrays.asList(command.split(""));
ProcessBuilder processBuilder = new ProcessBuilder(input);
BufferedReader bufferReader = null;
try {
Process proc = processBuilder.start();
InputStream inputStream = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(inputStream);
bufferReader = new BufferedReader(isr);
String line;
while ((line = bufferReader.readLine())!= null){
System.out.println(line );
}
bufferReader.close();
} catch (java.io.IOException ioe){
System.err.println("Error");
System.err.println(ioe);
} finally {
if (bufferReader != null){
bufferReader.close();
}
}
}
public static void main(String[] args) throws java.io.IOException {
String commandLine;
Scanner scanner = new Scanner(System.in);
System.out.println("
***** Welcome to the Java Command Shell *****");
System.out.println("If you want to exit the shell, type END and press RETURN.
");
while (true){
System.out.print("jsh>");
commandLine = scanner.nextLine();
// if user entered a return, just loop again
if (commandLine.equals("")){
continue;
}
if (commandLine.toLowerCase().equals("end")){//User wants to end shell
System.out.println("
***** Command Shell Terminated. See you next time. BYE for now. *****
");
scanner.close();
System.exit(0);
}
createProcess(commandLine);
}
}
} Programming tasks
Using the TestProcessBulder.java class as a base, implement task 1, task 2 and task 3 according to the descriptions below.
2.1 Task 1
In order to not block the command shell when a process is created, we will create a thread that manages the running of each application. Create a new class that implements the Runnable interface and which takes the string command as input in the constructor. This will be the class that calls ProcessBuilder in its run method. Observe that since we need to have access to the string command in the run method of the thread, we need to store this when the thread is created. In the run method we need to add the code from the method createProcess, then we need to modify the old method createProcess as follows: Instead of calling ProcessBuilder in this method you should create an instance of the class that implements Runnable. Then start this instance as a thread.
2.2 Task 2
Users may sometime enter a wrong or an invalid command. You may have noticed the error message you get when you enter an invalid command. In addition to displaying the error message, it is desired that a log of the error commands is kept in memory for system troubleshooting purposes. When the user enters the command showerrlog, the shell should display all erroneous commands entered by the user during the current session. The task is to modify the shell program to keep a log of erroneous commands and show it to the user when the showerrlog command is invoked. Remember that showerrlog is not an OS command. It is an enhancement you have just added! Hint! How can you detect that a command has been entered that cannot be run?
2.3 Task 3
In this task you will create a simple stopwatch that is console based, running in a separate thread.
Start with creating a new Java class that has a main method.
In the main method create a new thread and start it. The thread should do the following.
When starting the thread, the elapsed time should be set to zero.
Every 10ms, there should be a printout of the elapsed time in seconds. Use Thread.sleep()
When the elapsed time has reached 60 seconds, then the stopwatch thread should exit and join with the main thread. Use Thread.join().
Answer the following questions:
1. What does the Thread.join() do? What happens if we remove it from the main method?
2. Compare the real elapsed time using a stopwatch on your phone with your Java implemen-tation. Does the elapsed time differ, why?
3 Final observations
Also write down the following for each task to discuss during the seminar.
How did you implement the task?
Why did you solve it in the way you did it?
What difference in behaviour did you notice?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!