簡體   English   中英

通過Java程序使用Haskell的GHCI

[英]Using Haskell's GHCI through a Java Program

我想做的是將我的Java程序包裝在GHCI 在我看來,它應該像這樣工作:

  1. 啟動我的Java程序
  2. 寫下一些Haskell函數作為Java的輸入(即,反向[1,2,3,4])
  3. 在我的Java控制台上查看相應的Haskell輸出

因為我不想弄亂任何語言橋梁,所以嘗試了笨拙的方法並使用了Runtime.exec()方法。

這是我的代碼:

public static void main(String[] args) throws Exception {
Runtime r = Runtime.getRuntime();
Process p = r.exec("ghci");
OutputStream output = p.getOutputStream();
output.write("let x = 5\r\n".getBytes());
output.write("x".getBytes());

int tmp;
String result = "";
while ((tmp = p.getInputStream().read()) != -1) {
  result += (char) tmp;
}

System.out.println(result);
p.destroy();  }

我的問題是read()方法始終返回-1,而我無法獲得輸出。 我什至不知道我寫的內容是否創建了任何輸出。

幫助將不勝感激。 謝謝!

顯然, Process p = r.exec("ghci"); 未能成功執行read()方法始終返回-1 提供完整路徑並檢查。

Process p = r.exec("/fullpath/ghci  2>&1");
p.waitFor();//You need to use this line of code

首先確認,先執行ls命令

Process p = r.exec("ls 2>&1");

也可以像下面那樣修改您的代碼,然后嘗試:-

public static void main(String[] args) throws Exception {
    Runtime r = Runtime.getRuntime();
    Process p = r.exec("ghci");
            p.waitFor();
    OutputStream output = p.getOutputStream();
    ByteArrayOutputStream byte1=new ByteArrayOutputStream();
    output.write(byte1.toByteArray());
    String result=byte1.toString();
    System.out.println(result);
    p.destroy();  
}

暫無
暫無

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

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