4

I'm writing a program requiring communication between Java and Erlang using JInterface. I've a problem with receiving a list from an Erlang process - somehow the object I get in Java is not a OtpErlangList but OtpErlangString and if I try to cast the received object to OtpErlangList, I get a cast exception. I've tried decoding the string, but it seems not to be the case.

It seems to me rather odd not be able to send a list from Erlang to Java, could you please take a look if I'm not making any basic mistake?

Java fragment:

OtpErlangObject erlangObject = mailbox.receive();
OtpErlangList erlangList = (OtpErlangList) erlangObject;
System.out.println(erlangList.toString());

Erlang fragment:

List = [1, 2, 3, 4],
JavaPid ! List

I'm omitting the rest of the code as I believe these are the lines where the problem is - I've tried it with other classses and it worked.

przemek
  • 451
  • 4
  • 6

3 Answers3

2

In erlang, a string is just an array of small integer numbers. For example in erlang shell,

1> A = "abcdefgh".
"abcdefgh"
2> A ++ [1024].
[97,98,99,100,101,102,103,104,1024]
3> A ++ [105].
"abcdefghi"

So my guess is that this kind of list is always interpreted as string on Java side.

J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
2

From Jinterface documentations:

Lists in Erlang are also used to describe sequences of printable characters (strings). A convenience class OtpErlangString is provided to represent Erlang strings.

Getting String

For getting a string of printable characters in Java side, you should use stringValue() method which converts a list of integers into a Unicode string and returns a java.lang.String object.

Erlang side:

List = "hey" = [$h, $e, $y] = [104, 101, 121],
JavaPid ! List

Java side:

OtpErlangObject erlangObject = mailbox.receive();
OtpErlangList erlangList = (OtpErlangList) erlangObject;
System.out.println(erlangList.stringValue());

Getting Array

For getting a list of elements in Java side, you should use elements() method which returns an array containing all of the list's elements. This way each element of array is an object of type OtpErlangObject.

Erlang side:

List = [1, 2, 3, 4],
JavaPid ! List

Java side:

OtpErlangObject erlangObject = mailbox.receive();
OtpErlangList erlangList = (OtpErlangList) erlangObject;    
for(OtpErlangObject element : erlangList.elements()) {
    // do something with element
}
Hamidreza Soleimani
  • 2,504
  • 16
  • 19
0

Thanks to J.J. Hakala example, the answer is that if you send a list containing only integers that could be interpreted as ASCII codes, Java does exactly so - interpret it as an array of ASCII codes aka string:

            OtpErlangObject erlangObject = mailbox.receive();

            if (erlangObject instanceof OtpErlangString) {
                OtpErlangString string = (OtpErlangString) erlangObject;
                for(char c: string.stringValue().toCharArray()) {
                    int num = (int) c;
                    System.out.println(num);
                }
            }
przemek
  • 451
  • 4
  • 6