javaerlangelixirerlang-otpjinterface

Elixir / JInterface - Sending a list of strings back to elixir from Java


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!


Solution

  • 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).