Jinterface: communication between Java and Erlang using system file

Zabrane Mickael zabrane3@REDACTED
Thu Mar 17 22:02:17 CET 2011


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