2015-12-23 43 views
0

我正在創建一個程序,它將接受源代碼,編譯它並輸入測試用例以查看程序是否正確。一個源代碼檢查器,如果可以的話。我用來編譯程序的是通過cmd。我現在的問題是如果程序在cmd中運行,如何輸入測試用例。 這實際上是我們學校的一門課程。所以教授會給出一個問題(例如輸入一個整數,如果是偶數或奇數),那麼這個程序將通過測試教授提供的測試用例來檢查學生的源代碼(例如input:1 output:odd,輸入2:輸出:偶數)。如何在使用java在cmd中運行的程序中輸入

這裏是我的示例代碼(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(); 
     } 
+0

如何進入測試案例?你能提供關於這個問題的更多細節嗎? –

+0

@KevinHooke編輯:) – phoenixWright

回答

0

如果我得到了你的權利,你要啓動一個程序,並同時從Java注射輸入獲取其輸出方法。實際上這很簡單,因爲你已經在你的編譯方法中獲取了輸出。
Process Class還有一個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 
+0

對不起,我今天才能上網。所以,我試過後,沒有輸出,我不知道爲什麼。但我已經嘗試執行.exe文件,它說它已經打開,但仍然是,該程序沒有顯示任何輸出,並且「退出的值」沒有出現 – phoenixWright

+0

等待實際上它工作大聲笑。它首先在我的C#編譯器上嘗試過,但奇怪的是它沒有。然後我嘗試了您發佈的代碼並正確顯示。非常感謝您的幫助:D – phoenixWright

相關問題