[erlang-questions] Best way to interface Erlang with Java code

zabrane Mikael zabrane3@REDACTED
Thu Dec 10 19:06:57 CET 2009


Hi Garry!

Jayson write a nice tutorial to help me fix that at:
http://needlesslymessianic.com/2009/12/10/using-ports-to-communicate-with-external-programs

<http://needlesslymessianic.com/2009/12/10/using-ports-to-communicate-with-external-programs>Unfortunately,
it doesnt seem work with my Java program.

there's not much anyone can do to help with so little information.  we need
> to see stripped down code, description of what happens, etc.


So here are more details of what I want to achieve:

$ cat foo.txt
Hello World

# open the file "foo.txt", and printout what the file contains to "stdout"
$ java   my_java_program   foot.txt
Hello World

# notice the dash "-". When set, it means the same as before, but read from
stdin, and printout anything to stdout
$ cat foot.txt   |   java my_java_program  -
Hello World

What I'd like to get is very simple.
Send a string from an Erlang port to this java program and let it read it
from stdin (with "-").
Then, the java program send back the same string to the Erlang port.

Schematically
===========

% notice the "-" at the, I need to control the Java program and let it read
from "stdin" as shown before
Port = open_port({spawn, " java my_java_program   -  "}, [options goes
here]),
port_command(Port, "Hello World"),
loop(Port, <<>>).

and collecte what the "Java" program send back (I'm expecting the same
"Hello World"):

loop(Port, Bin) ->
receive
{Port, {data, Bin}} ->
 loop(Port, [Bin | Data]);
{Port, {exit_status, Status}} ->
       {exit_status, Status, list_to_binary([lists:reverse(Data)])};
Any ->
{error, Any}
end.

But this doesnt sems work at all.

Regards
Zabrane


More information about the erlang-questions mailing list