如何使用Java输入在cmd中运行的程序 [英] How to input in a program running in cmd using java

查看:43
本文介绍了如何使用Java输入在cmd中运行的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a program that will accept source codes, compile it, and input test cases to see whether the program was correct. A source code checker, if you may. What I used for compiling programs is through cmd. My problem now is how to enter the test cases once the programs are running in cmd. This is actually a program for our school. So the professor will give a problem (ex. Input an integer, say if it's even or odd) then this program will check the students' source codes by testing the test cases provided by the professor (ex. input: 1 output: odd, input 2: output: even).

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

Here's my sample code (c# compiler)

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程序视为学生源代码,该学生源代码将数字作为输入并检查数字是偶数还是奇数.

If I got you right you want to start a program and fetch its output while injecting input from your java method. Actually thats very simple since you already did the ouput fetching in your compile method.
The Process Class also has a getOutputStream method that you can use for injecting input to your process.
I will show how to do this with an example.
Consider this simple C programm as a students source code that takes a number as input and checks if it is even or odd.

#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 方法以运行应用程序,注入输入,读取输出并检查返回值.

Now implement your Run method like this to run the application, inject input, read output and check the return value.

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);
}

就这样.如果您像这样调用此方法

And thats it. If you invoke this method like this

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

这篇关于如何使用Java输入在cmd中运行的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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