Creating a CommandLine Shell in Java: Begginner help
Three questions that I can't seem to figure out.
1) I would like to create a new process using processbuilder to make it cat C:\xxx\xxx.java run the java file but I can't seem to implement that.
2) I get errors in my array while trying to compile
3) I can't figure out a clearscreen or a way to shut down the window when I type "clear" and "exit" respectively. system.exit(O) seems to only shut the virtual machine down and not actually close the window.
Here's my code, I'm sorry for being a suck but I need to get this done and I have no one to ask!
import java.io.*;
public class SimpleShell
{
public class JavaStringHistory
{
private String[] history = new String[4];
}
public static void main(String[] args) throws
java.io.IOException {
String commandLine;
BufferedReader console = new BufferedReader
(new InputStreamReader(System.in));
//Break with Ctrl+C
while (true) {
//read the command
System.out.print("shell>");
commandLine = console.readLine();
//if just a return, loop
if (commandLine.equals(""))
continue;
//history
if(commandLine.equals('*'))
{
//new class HistoryStringArray();
// {
// history[4] = history[3]
// history[3] = history[2]
// history[2] = history[1]
// history[1] = history[0]
// history[0] = commandLine
}
//help command
if (commandLine.equals("help"))
{
System.out.println();
System.out.println();
System.out.println("Welcome to the shell");
System.out.println("Written by: Brett Salmiery");
System.out.println("CIS 390 - Dr. Guzide");
System.ou开发者_如何转开发t.println("--------------------");
System.out.println();
System.out.println("Commands to use:");
System.out.println("1) cat prog.java");
System.out.println("2) exit");
System.out.println("3) clear");
System.out.println();
System.out.println();
System.out.println("---------------------");
System.out.println();
}
if (commandLine.equals("clear"))
{
if ( int cls = 0; cls < 10; cls++ )
{
System.out.print();
}
}
if (commandLine.endsWith(".java"))
{
if(commandLine.startsWith("cat"))
{
System.out.println("test");
ProcessBuilder pb = new ProcessBuilder();
//pb = new ProcessBuilder(commandLine);
}
else
{
System.out.println("Incorrect Command");
}
}
if (commandLine.equals("exit"))
{
System.out.println("...Terminating the Virtual Machine");
System.out.println("...Done");
System.out.println("Please Close manually with Options > Close");
System.exit(0);
}
}
}
}
if ( int cls = 0; cls < 10; cls++ )
I think you meant for
:)
As for the history mechanism: you've got a class JavaStringHistory
which has only one private member (not a bad start) but no methods to add entries to the history or retrieve entries from the history. So you ought to write those methods. (You could also make the member public and store into it directly as your commented-out code attempts to do, but you've already got a class for it, might as well finish the abstraction.)
And as for ProcessBuilder, I've got no idea how that works. I'm not sure what you'd want to accomplish with it, but you could use the FileReader
class to read the listed source file and print it to the terminal. (I might also suggest trying something like the String.split
method to pull apart your input string and look at each part individually.)
Closing the terminal window? Err. No idea there. Most profs are willing to recognize that sometimes goofy things happen with specific environments.
Hope this helps gets you started.
精彩评论