繁体   English   中英

如何使用Java输入在cmd中运行的程序

[英]How to input in a program running in cmd using java

我正在创建一个程序,该程序将接受源代码,对其进行编译并输入测试用例,以查看该程序是否正确。 源代码检查器,如果可以的话。 我用来编译程序的是通过cmd。 我现在的问题是,一旦程序在cmd中运行,如何输入测试用例。 这实际上是我们学校的课程。 因此,教授会提出问题(例如,输入一个整数,说它是偶数还是奇数),那么该程序将通过测试教授提供的测试用例来检查学生的源代码(例如,输入:1输出:奇数,输入2:输出:偶数)。

这是我的示例代码(C#编译器)

case ".cs":
            CsCompiler();
            Run(path + "\\program");
            break;

我的职能:

public static void CsCompiler() throws IOException, InterruptedException {
        Process(path + "\\", " c:\\Windows\\Microsoft.NET\\Framework\\v3.5\\csc /out:program.exe *.cs");
    }

public static void Process(String command, String exe) throws IOException, InterruptedException {
        final Process p;
        if (command != null) {
            p = Runtime.getRuntime().exec(exe, null, new File(command));
        } else {
            p = Runtime.getRuntime().exec(exe);
        }

        new Thread(new Runnable() {
            public void run() {
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line = null;

                try {
                    while ((line = input.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        p.waitFor();
    }

public static void Run(String command) throws IOException, InterruptedException {
            String[] argss = {"cmd", "/c", "start", command};

            ProcessBuilder pb;

            pb = new ProcessBuilder(argss);
            pb.start();
        }

如果我做对了,您想启动一个程序并在从您的java方法注入输入时获取其输出。 其实那很简单,因为您已经在编译方法中进行了输出提取。
Process类还具有getOutputStream方法,可用于将输入注入到流程中。
我将通过一个示例演示如何做到这一点。
将这个简单的C程序视为学生源代码,该学生源代码将数字作为输入并检查数字是偶数还是奇数。

#include <stdio.h>
int main(){
    int x;
    printf("Enter an Integer Number:\n");
    if (( scanf("%d", &x)) == 0){
        printf("Error: not an Integer\n");
        return 1;
    }
    if(x % 2 == 0) printf("even\n");
    else printf("odd\n");
    return 0;
}

现在,像这样实现您的Run方法来运行应用程序,注入输入,读取输出并检查返回值。

public static void Run(String command, String input) throws IOException, InterruptedException {
    // create process
    String[] argss = {"cmd", "/c", command};
    ProcessBuilder pb = new ProcessBuilder(argss);
    Process process = pb.start();        
    // create write reader
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));        
    // write input
    writer.write(input + "\n");
    writer.flush();
    // read output
    String line = "";
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    // wait for process to finish
    int returnValue = process.waitFor();
    // close writer reader
    reader.close();
    writer.close();
    System.out.println("Exit with value " + returnValue);
}

就是这样。 如果您这样调用此方法

Run("NumberChecker.exe", "1");
Run("NumberChecker.exe", "2");
Run("NumberChecker.exe", "a");

您将获得以下输出

Enter an Integer Number:
odd
Exit with value 0
Enter an Integer Number:
even
Exit with value 0
Enter an Integer Number:
Error: not an Integer
Exit with value 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM