0

What I would like to do is wrap my Java program around the GHCI. In my mind it should work like this:

  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

Because I did not want to mess around with any language bridges I tried the clumsy way and used the Runtime.exec() approach.

This is my Code:

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();  }

My problem here is that the read() method always returns a -1 and I cannot get the output. I dont even know if what I wrote created any Output.

Help would be appreciated. Thanks!

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
Mythos
  • 3
  • 2
  • Possible duplicate of [Printing Runtime exec() OutputStream to console](https://stackoverflow.com/questions/3936023/printing-runtime-exec-outputstream-to-console) – Ami Hollander Feb 23 '18 at 16:13
  • That Answer did not solve my problem because if I read from another Thread the read() method still returns -1 – Mythos Feb 23 '18 at 16:18

1 Answers1

2

It is clear that Process p = r.exec("ghci"); did not successful for which read() method always returns a -1. Provide full path and check.

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

For confirmation first execute ls command first

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

Also modify your codes like below and try:-

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();  
}
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
  • Adding the full path resulted in an error message that it wasnt able to find the specified file. I tried typing in just "ghci" in cmd (that worked) and in the bash VM (that worked as well). – Mythos Feb 23 '18 at 16:31
  • what it returns in standard ouput? – Abhijit Pritam Dutta Feb 23 '18 at 16:33
  • When I type the keyword "ghci" in my CMD I get this CMD output: GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help Prelude> Same for bash – Mythos Feb 23 '18 at 16:34
  • I have modified my answer. First just check with `ls` command. – Abhijit Pritam Dutta Feb 23 '18 at 16:43
  • Thank you for taking your time! Unfortunately my read() method still returns -1 immediately when doing as you said. When I type the ls command in cmd it outputs the appropriate list of directorys Edit: to clarify: neither the "/myPathToGHCI/ghci 2>&1" nor the "ls 2>&1" worked – Mythos Feb 23 '18 at 16:54
  • Check in your program what happening immediately after `OutputStream output = p.getOutputStream(); ` print the whole output stream and check immediately after above line. – Abhijit Pritam Dutta Feb 23 '18 at 17:01
  • Did not quite understand what you meant with "check whats happening". What I did is check if(output == null). Output wasnt null. I did the same with input and it wasnt null as well. When I System out print my output I get the usual object address – Mythos Feb 23 '18 at 17:07
  • I have modified my answer please see. Add `p.waitFor();` – Abhijit Pritam Dutta Feb 23 '18 at 17:16
  • Sorry for the late Answer! I had to switch computers because I was in University and using one of the networked PC's, I tried my original Code on my home PC and it turns out, everything is working as intended... I guess the University prevented process to process communication somehow in this instance. Thank you very much for your time! – Mythos Feb 23 '18 at 20:40