Java中的命令提示符Shell接口 [英] Command Prompt Shell Interface in Java

查看:65
本文介绍了Java中的命令提示符Shell接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拿了一个程序,有人为UNIX编写了类似终端的shell接口。但是,我想更改它,以便它可以模拟命令提示符,而不是终端。



程序编译,但如果我尝试,例如,运行在程序中简单地命令dir然后恐慌并退出。退出时显示与此类似的内容:



CreateProcess error = 2。系统找不到指定的文件。



唯一成功运行的命令是help命令(并且被授予,exit和 命令。



我将不胜感激任何有关以下计划的帮助:



I took a program that someone wrote for a terminal-like shell interface for UNIX. However, I would like to change this so it can emulate a command prompt, not a terminal.

The program compiles, but if I try, for example, running the simple command of "dir" within the program then it panics and exits. Something similar to this shows on exit:

CreateProcess error=2. The system cannot find the file specified.

The only command that runs successfully is the "help" command (and granted, the "exit" and "" commands.)

I would appreciate any help that could be given regarding the program below:

import java.io.*;
import java.util.*;

public class shell
{
    public static void main(String[] args) throws java.io.IOException {
        
        String commandLine;
        BufferedReader console = new BufferedReader
            (new InputStreamReader(System.in));
        
        while (true) {
            // read what the user entered
            System.out.print("My shell>");
            commandLine = console.readLine(); {
                // if the user entered a return, just loop again
                if (commandLine.equals("")) {
                    continue;
                }
                else if (commandLine.equalsIgnoreCase("exit")) {
                    System.out.println("Goodbye");
                    System.exit(0);
                }
                
                // split the string into a string array
                
                ArrayList<String> parms = new ArrayList<String>();
                String[] lineSplit = commandLine.split(" ");
                
                int size = lineSplit.length;
                for (int i=0; i<size; i++) {
                    parms.add(lineSplit[i]);
                }
                
                ProcessBuilder pb = new ProcessBuilder(parms);
                Process proc = pb.start();
                
                // obtain the input stream
                InputStream is = proc.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                
                // read what is returned by the command
                String line;
                while ((line = br.readLine()) != null)
                    System.out.println(line);
                    
                br.close();
                
            }
        }
    }
}

推荐答案

这样的命令因为dir是命令shell的内部,而不是独立程序。因此,您必须将它们发送到命令shell进程,您不能以您在代码中的方式调用它们。对于这个简单的程序,您可以尝试以这种方式将每个命令发送到shell。程序名称为 cmd.exe ,参数将是/ C的串联,实际命令name和该命令的参数。
Commands such as "dir" are internal to the command shell, and not freestanding programs. So you have to send them to a command shell process, you cannot invoke them in the way you have in your code. For this simple program you could just try sending every command to the shell in that way. The program name would be cmd.exe and the parameters would be a concatenation of "/C ", the actual command name, and the parameters to that command.


这篇关于Java中的命令提示符Shell接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆