1

I have elixir/otp app running. It needs to use some Java code so I use JInterface to achieve RPC-like communication.

I now have this communincation working. I can send a message from elixir to Java and conceptually send some data back.

I need to send back an array of strings.

This would seem like a straightforward task but I am struggling to find the right documentation to do this. The only info I can find only really gets me as far as sending back a binary string.

Here is the important bit of my JInterface code:

    private static void setupMBox() {
    try {
        OtpNode myOtpNode = new OtpNode("server");
        OtpMbox myOtpMbox = myOtpNode.createMbox("ltext");
        myOtpNode.setCookie("cookiepassword");

        while (true) {
            OtpErlangTuple tuple = (OtpErlangTuple) myOtpMbox.receive();
            OtpErlangPid lastPid = (OtpErlangPid) tuple.elementAt(0);
            OtpErlangAtom dispatch = (OtpErlangAtom) tuple.elementAt(1);

            if (dispatch.toString().equals("split_paragraph")) {
                List<String> sentences = paragraphSplitter.splitParagraphIntoSentences(TEST_PARAGRAPH, Locale.JAPAN);
                List<OtpErlangString> erlangStrings = new ArrayList<OtpErlangString>();

                for (String sentence : sentences) {
                    erlangStrings.add(new OtpErlangString(sentence));
                }
                // this will not work
                OtpErlangList erlangList = new OtpErlangList((OtpErlangObject[]) erlangStrings.toArray());

                myOtpMbox.send(lastPid, erlangList);
                System.out.println(erlangList);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

If anybody has done this and can help me I would really appreciate some guidance. Thanks in advance!

apostrophedottilde
  • 867
  • 13
  • 36

1 Answers1

1

Please state what's wrong with your code (what's the expected result and what's the actual result).

A little suggestion:

OtpErlangString actually maps to charlist in Erlang/Elixir, so this may not be what you want because charlist does not support unicode. Use OtpErlangBinary instead, and don't forget to map all java strings into byte arrays using javaString.getBytes(StandardCharsets.UTF_8), otherwise you won't get the UTF-8 encoded binaries in Erlang/Elixir because java internally encodes every string in UTF-16LE (due to the need to be compatible with Windows' fixed-bytes unicode encoding).

Aetherus
  • 8,720
  • 1
  • 22
  • 36
  • 1
    Ok, so I got it working shortly after I posted this. Have made many changes since then and can't remember specifically what the issue was - however, youve given me some interesting things to check that I wasnt aware of and I need to test this solution works for all cases. In particular, I didnt know about "UTF-16LE". My app is for learning and correcting foreign language text - so this stuff is pivotally important. Thanks for the wisdom :) – apostrophedottilde Jan 02 '20 at 16:44
  • 2
    @apostrophedottilde you should post here somehow the code you arrived at, so that people who read this will see what a solution looks like. – Sam Rose Dec 03 '21 at 17:15