java - 在 Java 中创建命令行 Shell : Begginner help

标签 java shell command-line

三个我似乎想不通的问题。

1) 我想使用 processbuilder 创建一个新进程,使其 cat C:\xxx\xxx.java 运行 java 文件,但我似乎无法实现它。

2) 我在尝试编译时在我的数组中遇到错误

3) 当我分别键入“clear”和“exit”时,我想不出一个 clearscreen 或关闭窗口的方法。 system.exit(O) 似乎只是关闭了虚拟机,并没有真正关闭窗口。

这是我的代码,很抱歉我很烂,但我需要完成这件事,我没有人可以问!

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.out.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++ )

我想你的意思是for :)

至于历史机制:你有一个 JavaStringHistory 类,它只有一个私有(private)成员(不错的开始),但没有向历史添加条目或从历史中检索条目的方法.所以你应该写那些方法。 (您也可以将成员公开并直接存储到其中,就像您注释掉的代码尝试做的那样,但是您已经为它创建了一个类,不妨完成抽象。)

至于 ProcessBuilder,我不知道它是如何工作的。我不确定你想用它完成什么,但你可以使用 FileReader类读取列出的源文件并将其打印到终端。 (我可能还建议尝试类似 String.split 的方法来拆分输入字符串并单独查看每个部分。)

关闭终端窗口?呃。不知道那里。大多数教授都愿意承认,有时在特定环境中会发生一些愚蠢的事情。

希望这有助于您入门。

关于java - 在 Java 中创建命令行 Shell : Begginner help,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4778132/

相关文章:

shell - bash 命令名称以 '-'(减号)开头

string - 在 CMake 中如何将命令的多行输出转换为列表?

java - 无法编译 Java 程序命令行

java - Documentum 5.3 连接

java - 尝试从小程序切换到 JFrame

java - 单击旁边的图标,删除 JCombobox 下拉列表中的项目

c++ - 如何从文件名列表创建 IShellItemArray

bash - 如何检查主机是否在您的 known_host ssh 中

linux - 在我的 .tcshrc 文件中以只读模式在 Linux 上为 emacs 创建别名

java - 从本地方法返回由 JNI 创建的本地引用