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!

2
  • Possible duplicate of Printing Runtime exec() OutputStream to console Commented Feb 23, 2018 at 16:13
  • That Answer did not solve my problem because if I read from another Thread the read() method still returns -1 Commented Feb 23, 2018 at 16:18

1 Answer 1

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();  
}
Sign up to request clarification or add additional context in comments.

9 Comments

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).
what it returns in standard ouput?
When I type the keyword "ghci" in my CMD I get this CMD output: GHCi, version 8.0.1: haskell.org/ghc :? for help Prelude> Same for bash
I have modified my answer. First just check with ls command.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.