[erlang-questions] link/1 appears to interfere with message sending

J David Eisenberg <>
Sat Mar 16 07:15:51 CET 2013


I have written a test program that connects Erlang and Java as follows:
1) Erlang module spawns a Java program via open_port/2.
2) Erlang then uses the port to send Java the Erlang node name.
3) The Java program uses that information to open a connection to Erlang.
4) Java sends back the pid of the Java program, then awaits a message
from Erlang
5) Erlang sends back an acknowledgment to Java, and...
6) Java sends back a final message to Erlang.

Here is the Java code with debugging output in Test.java
//==========================
import com.ericsson.otp.erlang.*;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Test
{
  public static void main(String[] args)
  {
    OtpSelf self;
    OtpConnection connection;
    OtpPeer erlangPeer;
    OtpErlangObject [] elements = new OtpErlangObject[2];
    OtpMsg message;
    OtpErlangObject obj;
    OtpErlangTuple tuple;
    OtpErlangPid erlangPid;
    String erlangAtom;

    try
    {
      self = new OtpSelf("java");
      BufferedReader input = new BufferedReader(new InputStreamReader(
        System.in));
      String erlNodeName = input.readLine();
      System.err.println("Erlang node is |" + erlNodeName + "|\r");

      erlangPeer = new OtpPeer(erlNodeName);

      connection = self.connect(erlangPeer);
      System.err.println("Connection to " + erlangPeer + " " +
connection + "\r");

      elements[0] = new OtpErlangAtom("sent_from_java");
      elements[1] = connection.self().pid();
      connection.send("testing", new OtpErlangTuple(elements));

      message = connection.receiveMsg();
      System.err.println("Java received message " + message + "\r");
      obj = message.getMsg();
      tuple = (OtpErlangTuple) obj;
      erlangAtom = ((OtpErlangAtom) tuple.elementAt(0)).atomValue();
      erlangPid = (OtpErlangPid) tuple.elementAt(1);
      System.err.println("Java received " + erlangAtom + " " +
erlangPid + "\r");

      connection.send("testing", new OtpErlangAtom("goodbye"));
    }
    catch (Exception e)
    {
      System.err.println("Java gets exception" + "\r");
      e.printStackTrace(System.err);
    }
  }
}
//===========================

And here is the Erlang module, test.erl:
%=====================
-module(test).
-export([go/0,test/0]).

go() ->
  Pid = spawn(?MODULE, test, []),
  register(testing, Pid),
  io:format("Erlang spawns test process with pid ~p~n", [Pid]),
  process_flag(trap_exit, true),
  Port = open_port({spawn, "java -cp
.:/usr/lib/erlang/lib/jinterface-1.5.6/priv/OtpErlang.jar:core.jar
Test"},
    [{line, 256}]),

  % send my node name to the Java program.
  port_command(Port, list_to_binary(atom_to_list(node()) ++ "\n" )),
  test().

test() ->
  receive
    % get the pid of the Java process
    {Atom, Pid} ->
      io:format("Erlang receives: ~p ~p~n", [Atom, Pid]),

      %%% link(Pid), % let me know if Java crashes..

      Pid ! {sent_from_erlang, self()}, % and send it a message
      test(); % then wait for another message
    Other ->
      io:format("Erlang receives ~p~n", [Other])
   end.
%======================

This all works great until I uncomment the ilne with the %%%
link(Pid). If I do that, then, when I send the message to Java, it
says:

java.lang.NullPointerException
at com.ericsson.otp.erlang.OtpMsg.getMsg(OtpMsg.java:212)
at Test.main(Test.java:39)

If I link to the Port instead of the Pid, everything works. I'm
clearly overlooking some simple concept; what is it?



More information about the erlang-questions mailing list