Jump to content

Help Writing a SimpleShell in Java (Not asking for code)

Hi all,

 

for my Operating Systems class I need to write a Simple Shell in Java.

It has several parts to it, and I am unsure on how to move forward. I want to understand the code of course, so I just need to be pointed in the right direction, or be told steps (and then I'll the code writing from there).

 

Part 1 Creating an External Process

 My professor would like us to use a mthod with the signature

 

public ProcessBuilder (List <String> command)

 

On the assignment sheet he says "a java.util.ArrayList which implements the list interface, can be used, where the first element of the list is a command and the second is the parameter"

 

So here is my understanding on what to do so far:

 

  • Obviously the ProcessBuilder method's parameter should be modified so its taking in an Arraylist.

 

  • In the provided sourcecode I should add the ArrayList commandIn and split the inputs so they parse to commandIn[1] and commandIn [2].

 

I'm unsure how to move forward from here.

 

 

Here is the source code my professor provided us, and that we're supposed to build on top of.

 

public class SimpleShell 
{
    public static void main (String [] args) throws IOException
            {
                String commandline;
                
                BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
                
                while(true)
                {
                    System.out.println("jsh>");
                    commandline = console.readLine();
                    
                    if ( commandline.equals(" "))
                        continue;
                }
            }

Link to post
Share on other sites

33 minutes ago, BrownZeus said:

Obviously the ProcessBuilder method's parameter should be modified so its taking in an Arraylist.

Not exactly, List is an interface and ArrayList is a class than implements the interface. So the method will work as is. Easiest way you can think of it is List is a contract that says any "List" has to have these methods. In order for ArrayList to call itself a list, it has to abide by that contract but that's irrelevant to you - you're saying I'll take any List and your instructor is saying here's an ArrayList. Since it has all the methods, it will just work with your code as long as you stick to using List methods.

 

36 minutes ago, BrownZeus said:

In the provided sourcecode I should add the ArrayList commandIn and split the inputs so they parse to commandIn[1] and commandIn [2].

Yeah, basically what he's asking is to split up the command, put all the stuff in an ArrayList and pass it on to the ProcessBuilder. That's pretty much it. Since you're already reading the line, it's a matter of figuring out how to split a string and put in an ArrayList - since you mentioned you're not looking for code, I'll let you take the reins on this one from here ;)

Link to post
Share on other sites

10 minutes ago, Dobri said:

Not exactly, List is an interface and ArrayList is a class than implements the interface. So the method will work as is. Easiest way you can think of it is List is a contract that says any "List" has to have these methods. In order for ArrayList to call itself a list, it has to abide by that contract but that's irrelevant to you - you're saying I'll take any List and your instructor is saying here's an ArrayList. Since it has all the methods, it will just work with your code as long as you stick to using List methods.

 

Yeah, basically what he's asking is to split up the command, put all the stuff in an ArrayList and pass it on to the ProcessBuilder. That's pretty much it. Since you're already reading the line, it's a matter of figuring out how to split a string and put in an ArrayList - since you mentioned you're not looking for code, I'll let you take the reins on this one from here ;)

What is the purpose of the BufferedReader? I'm still a little hung up on that. I know that the .read command just reads(and therefore I'm assuming interprets) The assignment sheet also says that I should throw an error message if input is incorrect (a command isn't supported/not found) So should I be using the bufferedreader to do input validation?

Link to post
Share on other sites

2 minutes ago, BrownZeus said:

What is the purpose of the BufferedReader? I'm still a little hung up on that. I know that the .read command just reads(and therefore I'm assuming interprets) The assignment sheet also says that I should throw an error message if input is incorrect (a command isn't supported/not found) So should I be using the bufferedreader to do input validation?

A BufferedReader "Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines." Use Java docs, they are your friends. Basically, you don't care about it and you think of it as a blackbox around the console input that you can call "readLine" on that will block until a line is provided, i.e. you press Enter.

56 minutes ago, BrownZeus said:

new BufferedReader(new InputStreamReader(System.in));

If you care about more details - System.in is the system's console input channel. InputStreamReader takes an input channel and creates a stream reader from it, i.e. you can call basic reads to fetch however many bytes are available at the time of the call. BufferedReader takes a StreamReader and buffers it for easier reading (so you can read lines and more efficiently read chunks, etc.) by simply adding a buffer and calling the StreamReader's basic read methods.

 

WRT validation - I don't think your professor provides enough information for you to be able to know what command are or are not recognized (since the actual ProcessBuilder is given to you as a blackbox) but perhaps from the way you've described the problem he's asking you to validate there's exactly two things in the ArrayList you generate? So, for example, "bar foo" is a valid input but "bar foo biz" or "foo" are not.

Link to post
Share on other sites

3 minutes ago, Dobri said:

A BufferedReader "Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines." Use Java docs, they are your friends. Basically, you don't care about it and you think of it as a blackbox around the console input that you can call "readLine" on that will block until a line is provided, i.e. you press Enter.

If you care about more details - System.in is the system's console input channel. InputStreamReader takes an input channel and creates a stream reader from it, i.e. you can call basic reads to fetch however many bytes are available at the time of the call. BufferedReader takes a StreamReader and buffers it for easier reading (so you can read lines and more efficiently read chunks, etc.) by simply adding a buffer and calling the StreamReader's basic read methods.

 

WRT validation - I don't think your professor provides enough information for you to be able to know what command are or are not recognized (since the actual ProcessBuilder is given to you as a blackbox) but perhaps from the way you've described the problem he's asking you to validate there's exactly two things in the ArrayList you generate? So, for example, "bar foo" is a valid input but "bar foo biz" or "foo" are not.

I'm thinking maybe I have to make a list of commands (commandList) i wanna support, and then compare the command input to my list of supported commands. So just commandIn[0] would be compared to commandList

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×