gpt4 book ai didi

java - 通过 Java 程序使用 Haskell 的 GHCI

转载 作者:行者123 更新时间:2023-12-02 02:18:10 25 4
gpt4 key购买 nike

我想做的是将我的 Java 程序包装在 GHCI 周围。在我看来,它应该像这样工作:

  1. Starting my Java program
  2. Write down some Haskell function as Input for Java (i.e. reverse [1,2,3,4])
  3. See the appropriate Haskell Output on my Java Console

因为我不想搞乱任何语言桥梁,所以我尝试了笨拙的方法并使用了 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();
}

关于java - 通过 Java 程序使用 Haskell 的 GHCI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48951936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com