[erlang-questions] Jinterface: communication between Java and Erlang using system file

Alain O'Dea alain.odea@REDACTED
Thu Mar 17 23:10:40 CET 2011


Hi Zabrane:

On 2011-03-17, at 18:32, Zabrane Mickael <zabrane3@REDACTED> wrote:

> Hi guys,
> 
> Please, have a look to the following simple Java code
> which try to encode a simple Erlang tuple and save it to disk.
> 
> -8-8-8-8-8-8-8-8-8-8 snip -8-8-8-8-8-8-8-8-8-8
> 
> // encode a simple Erlang tuple {ok, "Hello World"}
> OtpErlangObject[] reply = new OtpErlangObject[2];
> reply[0] = new OtpErlangAtom("ok");
> reply[1] = new OtpErlangBinary("Hello World!");
> OtpErlangTuple tuple = new OtpErlangTuple(reply);
> 
> // create an OtpOutputStream object from the binary
> OtpErlangBinary binary = new OtpErlangBinary(tuple);
> OtpOutputStream oos = new OtpOutputStream(binary);
> 
> oos.writeTo(fos);
> 
> // write the Erlang binary term to a file
> File f = new File ("a.txt");
> FileOutputStream fos = new FileOutputStream(f);
> 
> fos.flush();
> fos.close();
> 
> -8-8-8-8-8-8-8-8-8-8 snip -8-8-8-8-8-8-8-8-8-8
> 
> When trying to read it from disk, I got an exception:
> $ erl
> 1> {ok, Bin} = file:read_file("a.txt").
> {ok,<<109,0,0,2,17,172,237,0,5,115,114,0,38,99,111,109,
>      46,101,114,105,99,115,115,111,110,46,111,...>>}
> 2> binary_to_term(Bin).                
> ** exception error: bad argument
>     in function  binary_to_term/1
>        called as binary_to_term(<<109,0,0,2,17,172,237,0,5,115,114,0,38,99,111,109,46,
>                                   101,114,105,99,115,115,111,110,46,111,116,...>>)
> 
> 
> What am I doing wrong?
> Help appreciated ...
> 
> Regards,
> Zab

I published http://concise-software.blogspot.com/2009/08/concise-howtos-message-from-java-to.html a while back which uses node-to-node communication between Java and Erlang.  It may be better suited to your problem than using files as it naturally handles the marshalling an unmarshalling if messages in both directions.


More information about the erlang-questions mailing list