2012-01-09 107 views
0

我想從Web服務器運行Dos程序。 Dos程序必須以交互方式運行,因爲用戶界面是通過一系列問題和答案進行的。一個問題的答案將決定下一個問題。我將不得不在Web服務器上使用ajax,但我認爲我可以做到這一點。從java運行交互式Dos程序

我在Stackoverflow上找到了一個java程序,它似乎做了類似於我想要的東西。但是,當我編譯程序時,我得到一個錯誤,即。

javac PipeRedirection.java 
PipeRedirection.java:43: package InputProcess does not exist 
        InputProcess.Gobbler outGobbler = new InputProcess.Gobbler(p.getInputStream()); 

堆棧溢出問題的網址是

How can I write large output to Process getOutputStream?

Java文件是

/* 
####### PipeRedirection.java 
*/ 

import java.io.BufferedOutputStream; 
import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.util.ArrayList; 
import java.util.List; 

public class PipeRedirection { 

public static void main(String[] args) throws FileNotFoundException { 

    if(args.length < 2) { 
      System.err.println("Need at least two arguments"); 
      System.exit(1); 
    } 

    try { 
      String input = null; 
      for(int i = 0; i < args.length; i++) { 

        String[] commandList = args[i].split(" "); 

        ProcessBuilder pb = new ProcessBuilder(commandList); 
        //pb.redirectErrorStream(true); 
        Process p = pb.start(); 

        if(input != null) { 
          PrintWriter writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream())), true); 
          writer.println(input); 
          writer.flush(); 
          writer.close(); 
        } 

        InputProcess.Gobbler outGobbler = new InputProcess.Gobbler(p.getInputStream()); 
        InputProcess.Gobbler errGobbler = new InputProcess.Gobbler(p.getErrorStream()); 
        Thread outThread = new Thread(outGobbler); 
        Thread errThread = new Thread(errGobbler); 
        outThread.start(); 
        errThread.start(); 

        outThread.join(); 
        errThread.join(); 

        int exitVal = p.waitFor(); 
        System.out.println("\n****************************"); 
        System.out.println("Command: " + args[i]); 
        System.out.println("Exit Value = " + exitVal); 
        List<String> output = outGobbler.getOuput(); 
        input = ""; 
        for(String o: output) { 
          input += o; 
        } 
      } 
      System.out.println("Final Output:"); 
      System.out.println(input); 

    } catch (IOException ioe) { 
      // TODO Auto-generated catch block 
      System.err.println(ioe.getLocalizedMessage()); 
      ioe.printStackTrace(); 
    } catch (InterruptedException ie) { 
      // TODO Auto-generated catch block 
      System.err.println(ie.getLocalizedMessage()); 
      ie.printStackTrace(); 
    } 

} 


public static class Gobbler implements Runnable { 
    private BufferedReader reader; 
    private List<String> output; 

    public Gobbler(InputStream inputStream) { 
      this.reader = new BufferedReader(new InputStreamReader(inputStream)); 
    } 

    public void run() { 
      String line; 
      this.output = new ArrayList<String>(); 
      try { 
        while((line = this.reader.readLine()) != null) { 
          this.output.add(line + "\n"); 
        } 
        this.reader.close(); 
      } 
      catch (IOException e) { 
        // TODO 
        System.err.println("ERROR: " + e.getMessage()); 
      } 
    } 

    public List<String> getOuput() { 
      return this.output; 
    } 
} 
} 

有誰知道爲什麼我得到的編譯錯誤?我可以用一些其他代碼替代InputProcess嗎?

感謝所有幫助

彼得

回答

1

我認爲這是很明顯,你缺失部分這一代碼。名爲InputProcess的軟件包中有一個名爲Gobbler的類未包含在OP的文章中。可能是因爲它與他們的問題無關。

錯誤消息基本上說它找不到它正在尋找的這個包/代碼。

這個班級做了什麼,只有OP能告訴你。不過,最基本的看起來是從InputStream中讀取並將其轉換爲List<String>。我會閱讀Java IO並嘗試複製類似的功能。

編輯:

貌似Gobbler類確實包括在上面的例子中。從您的代碼中刪除InputProcess包名(或將Gobbler類包含在InputProcess包中),您應該很好。

+0

我讀了你的評論,然後我意識到戈布勒定義如上。我剛剛刪除了「InputProcess」。從上面的代碼,現在它編譯和運行良好。感謝您觸發答案。乾杯 – Peter 2012-01-09 01:48:17