phoenixWright
phoenixWright

Reputation: 77

How to input in a program running in cmd using java

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

Here's my sample code (c# compiler)

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

My functions:

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

Upvotes: 0

Views: 2654

Answers (1)

ArcticLord
ArcticLord

Reputation: 4039

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

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

You will get the following output

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

Upvotes: 2

Related Questions