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 createProcessString command throws java.ioIOException
List input Arrays.asListcommandsplit;
ProcessBuilder processBuilder new ProcessBuilderinput;
BufferedReader bufferReader null;
try
Process proc processBuilder.start;
InputStream inputStream proc.getInputStream;
InputStreamReader isr new InputStreamReaderinputStream;
bufferReader new BufferedReaderisr;
String line;
while line bufferReader.readLine null
System.out.printlnline ;
bufferReader.close;
catch javaioIOException ioe
System.err.printlnError;
System.err.printlnioe;
finally
if bufferReader null
bufferReader.close;
public static void mainString args throws java.ioIOException
String commandLine;
Scanner scanner new ScannerSystemin;
System.out.println
Welcome to the Java Command Shell ;
System.out.printlnIf you want to exit the shell, type END and press RETURN.
;
while true
System.out.printjsh;
commandLine scanner.nextLine;
if user entered a return, just loop again
if commandLineequals
continue;
if commandLinetoLowerCaseequalsendUser wants to end shell
System.out.println
Command Shell Terminated. See you next time. BYE for now.
;
scanner.close;
System.exit;
createProcesscommandLine;
Programming tasks
Using the TestProcessBulder.java class as a base, implement task task and task according to the descriptions below.
Task
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.
Task
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?
Task
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 ms there should be a printout of the elapsed time in seconds. Use Thread.sleep
When the elapsed time has reached seconds, then the stopwatch thread should exit and join with the main thread. Use Thread.join
Answer the following questions:
What does the Thread.join do What happens if we remove it from the main method?
Compare the real elapsed time using a stopwatch on your phone with your Java implementation. Does the elapsed time differ, why?
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
