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

标签 java shell haskell

我想做的是将我的 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/

相关文章:

java将输入复制到多个输出文件

java - @JsonFormat 不适用于自定义序列化器

haskell - 排序功能似乎并没有打破联系

java - 当我们在 Liferay 中说“组”时,这意味着什么?“组”由什么组成?及其用途?

bash - 如何使用bash匹配不同目录下的文件名

linux - 返回文件行数的 Bash shell 命令?

javascript - nodejs child_processes.exec 无法返回 'nohup' 命令

haskell - 逐步推导类型

haskell,数据二叉树的仿函数

java - 找出数组是否包含唯一元素