Jinterface: communication between Java and Erlang using system file

Nico Meyer nico.meyer@REDACTED
Fri Mar 18 19:50:45 CET 2011


Hi Zabrane,

the external erlang term format always starts with a decimal 131 byte
( http://www.erlang.org/doc/apps/erts/erl_ext_dist.html ). Seems like
that is missing from the file.

try to add:

fos.write(131);

somewhere before 
oos.writeTo(fos);

Cheers,
Nico

Am Donnerstag, den 17.03.2011, 22:02 +0100 schrieb Zabrane Mickael:
> 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
> 
> 




More information about the erlang-questions mailing list