簡體   English   中英

在Java中使用命令提示符

[英]Using command prompt with Java

我正在Windows 7計算機上工作。

我正在開發一個應用程序,它是Haskell的GHCi解釋器的前台。 用戶將輸入命令,然后Java將在運行時通過exec()方法執行命令,然后應用程序將顯示如果用戶僅使用命令提示符運行GHCi時將顯示的文本。

現在,我在打印輸出的循環方面遇到了問題。

這是我現在擁有的代碼。

public class GHCiTest {
public static Scanner rd, sc;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

try {

  System.out.println("Starting... ");

  Process p = Runtime.getRuntime().exec("ghci");

  PrintStream hugsin  = new PrintStream(p.getOutputStream());
  InputStream hugsout = p.getInputStream();

  sc = new Scanner(hugsout);
  rd = new Scanner(System.in);

  String rdnextline;



  while (true){
    while (sc.hasNextLine()){
        System.out.println(sc.nextLine());

    }
      System.out.println("yay");
      rdnextline = rd.nextLine();
    if (rdnextline == "quit"){break;}
    hugsin.println(rdnextline);
    hugsin.flush();

  }
  System.out.println(" ... successful completion.");
}
catch(IOException e) {
  e.printStackTrace();
}
}

}

我知道GHCi的初始啟動正在工作,因為該程序正在打印“ GHCi,版本7.10.3: http//www.haskell.org/ghc/ :?以尋求幫助”。 但是,問題似乎是while(sc.hasNextLine())循環,它應該讀取命令提示符的輸出並輸出直到沒有剩余的內容,因為它不會脫離循環並繼續執行。閱讀用戶輸入。 我知道這是因為程序沒有打印循環后放入的“是”標志。

直到流的末尾,循環才會退出:

while (sc.hasNextLine()){
    System.out.println(sc.nextLine());
}

流的末尾是您過程的結束。 因此,您的Java程序正在等待子進程運行完成並終止。 一旦發生這種情況,循環將結束,Java程序將向該進程發送所需的命令。

對不起,我的意思是,嘗試將所需的命令發送到該進程。 它不會成功,因為該過程已終止。

如果GHCi進程輸出某種“提示”,您可以嘗試在此時中斷while(...) { print } ,從用戶那里獲取輸入,將其發送給該進程,然后循環返回並重新輸入。 -輸入while(...) { print } ,等待下一個提示。

假設提示不是以換行符結尾,而是出現在鍵入用戶輸入的行的開頭,則不能使用while(sc.hasNextLine()) { ... }類型的循環,因為提示不是完整的一行。 您可能需要逐個字符地讀取字符,在最后的“ n”個字符中查找提示序列。

看起來您可以在GHCi中更改提示。 有關詳細信息,請參見此處 如果將提示更改為以換行符結尾,則仍可以按行讀取流。

while (sc.hasNextLine()){
    String line = sc.nextLine();
    if (line.equals("YourPromptHere"))
         break;
    System.out.println(line);
}

(或者,您可能可以對線程進行某些操作,以使兩個部分都可以運行而不會互相阻塞。當然,線程具有其自身的問題和復雜性。)


編輯

我眼前一亮。 假設GHC的提示如下所示...

 GHCi, version 7.10.3 yada, yada, yada ... Main> _ 

...您可以將掃描儀的分隔符設置為提示字符串Main>

// Set scanner delimiter to GHCi's Prompt string
sc = new Scanner(hugsout).setDelimiter("^Main> ");

while (sc.hasNext()) {

    // Echo GHCi's output upto the delimiter (prompt)
    System.out.println(sc.next());

    // Read user input & transfer to GHCi.
    System.out.print("Replacement Prompt> ");
    rdnextline = rd.nextLine();
    if (rdnextline == "quit") {
        break;
    }
    hugsin.println(rdnextline);
    hugsin.flush();
 }

注意 :這沒有考慮到輔助提示,當GHCi希望有更多輸入來完成命令時使用該輔助提示。 您可以使用類似於"^Main> |\\bAlt> "類的正則表達式來匹配任何一個提示,但是您將無法確定分隔符與哪個提示匹配。 第一個子表達式"^Main> "匹配行的開頭,后跟“ Main>”,第二"\\bAlt> "表達式"\\bAlt> "僅匹配單詞邊界,后跟“ Alt>”。 這是因為GHCi的輸出流看起來像"\\nMain> Alt> " ,在Alt>之前有很長的停頓。 Alt>之前的“換行符”通常來自輸入流中按下Enter鍵的回顯。

在另一個這樣的線程中接收ghci的輸出。

System.out.println("Starting... ");

Process p = Runtime.getRuntime().exec("ghci");

PrintStream hugsin = new PrintStream(p.getOutputStream());
InputStream hugsout = p.getInputStream();
Scanner rd = new Scanner(System.in);
new Thread(() -> {
    try (Reader r = new InputStreamReader(hugsout)) {
        int ch;
        while ((ch = r.read()) != -1)
            System.out.print((char)ch);
    } catch (IOException e ) {}
}).start();
Scanner sc = new Scanner(hugsout);
String rdnextline;
while (true) {
    rdnextline = rd.nextLine();
    hugsin.println(rdnextline);
    hugsin.flush();
    if (rdnextline.equals("quit")) {
        break;
    }
}
System.out.println(" ... successful completion.");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM