2016-06-12 32 views
0

我想編譯,然後通過Windows命令行運行程序。不能看到通過Java運行時輸出的輸出

我有以下代碼,我嘗試了我評論的線程方法,然後使用單一過程方法。生成類文件並希望執行生成hello world。我如何在一個字符串中捕獲這個。請注意,這是我正在使用的Windows機器。

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Scanner; 

public class CompilerMain { 

    public String doCompile(String compilationLang, String codeString, 
          String challengeName) throws InterruptedException, IOException { 

    String output = null; 
    String commandString = null; 

    if (compilationLang == "java") { 

     String path = "c:\\Test\\"; 

     challengeName = "Solution"; 
     String commandStringCompile; 
     String commandStringExcecute; 
     commandStringCompile = "javac " + path + challengeName + ".java"; 
     commandStringExcecute = "java " + path + challengeName; 
     System.out.println("Command is " + commandStringCompile); 
     System.out.println("Command is " + commandStringExcecute); 
     final Process p = Runtime.getRuntime().exec(commandStringCompile); 
     final Process q = Runtime.getRuntime().exec(commandStringExcecute); 
     Scanner err = new Scanner(p.getInputStream()); 
     Scanner in = new Scanner(q.getInputStream()); 


     while (in.hasNext()) { 

     System.out.println(in.next()); 

     } 

     while (err.hasNext()) { 

     System.out.println(err.next()); 

     } 

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


     /*   
     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(); 
     if(p.exitValue()==0){ 
      System.out.println("Error! Code " + p.exitValue()); 
      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(); 
      } 
     } 
     if(p.exitValue()!=0){ 
      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(); 
      } 
     } 
     final Process q = Runtime.getRuntime().exec(commandString); 
     commandString = "java "+ challengeName + " >>out.txt"; 
     new Thread(new Runnable() { 
      public void run() { 

       BufferedReader input = new BufferedReader(
         new InputStreamReader(q.getInputStream())); 
       String line = null; 

       try { 
        while ((line = input.readLine()) != null) { 
      //   System.out.println(" 1"); 

         System.out.println(line); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }).start(); 

     q.waitFor(); 
     System.out.println("Exit Value is " + q.exitValue()); 
    */ 
    } 
    return output; 
    } 
} 

回答

1

您的方法正常工作。這可能是你的命令字符串不正確。請嘗試從讀取p.getErrorStream()q.getErrorStream(),你會看到發生了什麼。

我改變你的代碼在我的Linux機器上工作了一下。

public String doCompile(String compilationLang, String codeString, 
         String challengeName) throws InterruptedException, IOException { 

    String output = null; 
    String commandString = null; 

    if (compilationLang == "java") { 

    String path = "/home/john/IdeaProjects/Test1/src/"; 

    String commandStringCompile; 
    String commandStringExcecute; 
    commandStringCompile = "javac " + path + "Main.java"; 
    commandStringExcecute = "java -cp " + path +" Main"; 
    System.out.println("Command is " + commandStringCompile); 
    System.out.println("Command is " + commandStringExcecute); 
    final Process p = Runtime.getRuntime().exec(commandStringCompile); 
    final Process q = Runtime.getRuntime().exec(commandStringExcecute); 
    Scanner err = new Scanner(p.getErrorStream()); 
    Scanner in = new Scanner(p.getInputStream()); 
    while (in.hasNext()) { 
     System.out.println(in.next()); 
    } 
    while (err.hasNext()) { 
     System.out.println(err.next()); 
    } 

    err = new Scanner(q.getErrorStream()); 
    in = new Scanner(q.getInputStream()); 
    while (in.hasNext()) { 
     System.out.println(in.next()); 
    } 
    while (err.hasNext()) { 
     System.out.println(err.next()); 
    } 
    } 
    return output; 
} 

輸出:

Command is javac /home/john/IdeaProjects/Test1/src/Main.java 
Command is java -cp /home/john/IdeaProjects/Test1/src/ Main 
hello 
world! 
+0

我一直無法覈實,我可以看到錯誤流好嗎,但從來沒有輸出流,也許這是一個Windows的事情。讓我試着在Linux平臺上運行它。 – Sameer