problem processing standard input/output from an external erlang command

323 views
Skip to first unread message

Irina Guberman

unread,
May 15, 2011, 5:08:14 PM5/15/11
to erlan...@googlegroups.com

Hi there,

I would greatly appreciate your help with the following.   I need to run an erl command from a java process and be able to get input from the java program (which is going to stdin) and then send output to stdout (that's what the java process will be expecting). 

Something like that:

java -jar inputOutput.jar -exec "erl stdin_reader execute"


So, essentially all I need to do is read stdin into my erl process (no prompt) and then write something to stdout:


-module(stdin_reader).
-export([execute/0]).

execute() ->
  Line = io:get_line(""),      %% I also tried this:  Line = io:get_line(standard_io, "")
  io:write(io:format("Got " ++ Line)).


However, after I compile the module and run the following command I get this error:

C:\ErlangSB>echo hello | erl stdin_reader execute
Eshell V5.8.1  (abort with ^G)
1> * 1: syntax error before:
1>
C:\ErlangSB>


Also, if I try to send it an input file input.txt with contents

123
456

The error is:

C:\ErlangSB>type input.txt | erl stdin_reader execute
Eshell V5.8.1  (abort with ^G)
1> * 2: syntax error before: 456
1> *** Terminating erlang (nonode@nohost)


I also tried a simpler module where all I was doing was type something to the stdout.   However running this command takes me to the erlang shell and doesn't print anything out.   Clearly, I have some basic misunderstanding of how this works:

-module(simple_writer).
-export([execute/0]).

execute()->io:write("Write something already").

C:\ErlangSB>erl simple_writer execute
Eshell V5.8.1  (abort with ^G)
1>


Any ideas?

Thanks a lot!
Irina.


Garrett Smith

unread,
May 16, 2011, 12:29:41 AM5/16/11
to erlan...@googlegroups.com
Try this in a file named "erlecho":

#!/usr/bin/env escript
%%% -*-erlang-*-

main(_) ->
{ok, S} = io:fread("", "~s"),
io:format("~s~n", [S]).

If you're on Windows, drop the first two lines (i.e. you just need the
main/1 function).

Then run:

echo "yo Irina" | escript erlecho

I *think* this is what you're asking for :) See the docs on io:fread/2
- it's a nice interface to stdin I think.

There are some other interesting ways to get Java to talk to Erlang
(e.g. jinterface - see
http://www.erlang.org/doc/apps/jinterface/users_guide.html) and good
ol' REST, but of course that's all quite a bit more involved.

Irina Guberman

unread,
May 17, 2011, 11:12:44 PM5/17/11
to erlan...@googlegroups.com
Yes, perferct!  That's exactly what I want :)

Many thanks!
Irina.
Reply all
Reply to author
Forward
0 new messages